Source code for submission s882

Main.java

  1.  
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.util.StringTokenizer;
  5.  
  6. public class Main {
  7.  
  8. protected String line;
  9. protected final int MODULE = 1000000;
  10.  
  11. public static void main(String[] args) throws Exception {
  12. Main p = new Main();
  13. p.run();
  14. }
  15.  
  16. protected void run() throws Exception {
  17. while (input.ready()) {
  18. line = nextLine();
  19. if (line.charAt(0) == ')') {
  20. System.out.println("0");
  21. } else {
  22. System.out.println(computeResults(1, 0, 1));
  23. }
  24. }
  25. }
  26.  
  27. protected int computeResults(int fiveCount, int tenCount, int position) {
  28. if (fiveCount > line.length() / 2) {
  29. return 0;
  30. }
  31. if (position == line.length()) {
  32. if (fiveCount == tenCount) {
  33. return 1;
  34. }
  35. return 0;
  36. }
  37. char c = line.charAt(position);
  38. if (c == '(') {
  39. return computeResults(fiveCount + 1, tenCount, position + 1);
  40. }
  41. if (c == ')') {
  42. if (fiveCount == tenCount) {
  43. return 0;
  44. }
  45. return computeResults(fiveCount, tenCount + 1, position + 1);
  46. }
  47. int res = 0;
  48. if (fiveCount > tenCount) {
  49. res += computeResults(fiveCount, tenCount + 1, position + 1);
  50. }
  51. res += computeResults(fiveCount + 1, tenCount, position + 1);
  52. return res % MODULE;
  53. }
  54.  
  55.  
  56. public String nextToken() throws Exception {
  57. while (!st.hasMoreTokens()) {
  58. st = new StringTokenizer(input.readLine());
  59. }
  60. return st.nextToken();
  61. }
  62.  
  63. public int nextInt() throws Exception {
  64. return Integer.parseInt(nextToken());
  65. }
  66.  
  67. public String nextLine() throws Exception {
  68. return input.readLine();
  69. }
  70.  
  71. }
  72.