Source code for submission s721

Go to diff to previous submission

FQ.java

  1.  
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5.  
  6. /*
  7.  * To change this template, choose Tools | Templates
  8.  * and open the template in the editor.
  9.  */
  10.  
  11. /**
  12.  *
  13.  * @author cteam94
  14.  */
  15. public class FQ {
  16. int n;
  17. String line;
  18. Integer[][] a;
  19.  
  20. public static void main(String[] args) throws IOException {
  21.  
  22.  
  23. while((new FQ()).readLine(br));
  24. }
  25.  
  26. public boolean readLine(BufferedReader br) throws IOException {
  27. line = br.readLine();
  28. if (line == null) return false;
  29.  
  30. n = line.length();
  31. a = new Integer[n/2 + 1][n/2 + 1];
  32. for (int i = 0; i < a.length; i++) {
  33. for (int j = 0; j <=i; j++) {
  34. a[i][j] = -1;
  35. }
  36.  
  37. }
  38.  
  39. int x = p(0, 0);
  40. System.out.println(x % 1000000);
  41.  
  42. return true;
  43. }
  44.  
  45. public int p(int o, int c){
  46. if (o<c || o>n/2) return 0;
  47. if (o+c==n) return 1;
  48. if (a[o][c]==-1){
  49. if (line.charAt(o+c)=='(') a[o][c] = p(o+1,c);
  50. if (line.charAt(o+c)==')') a[o][c] = p(o,c+1);
  51. if (line.charAt(o+c)=='.') a[o][c] = p(o+1,c) + p(o,c+1);
  52. }
  53.  
  54. return a[o][c];
  55. }
  56. }
  57.  

Diff to submission s672

FQ.java

--- c5.s672.cteam094.fq.java.0.FQ.java
+++ c5.s721.cteam094.fq.java.0.FQ.java
@@ -16,8 +16,10 @@
     int n;
     String line;
+    Integer[][] a;
     
     public static void main(String[] args) throws IOException {
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         
+        
         while((new FQ()).readLine(br));
     }
@@ -28,4 +30,11 @@
         
         n = line.length();
+        a = new Integer[n/2 + 1][n/2 + 1];
+        for (int i = 0; i < a.length; i++) {
+            for (int j = 0; j <=i; j++) {
+                a[i][j] = -1;
+            }
+            
+        }
         
         int x = p(0, 0);
@@ -38,8 +47,11 @@
         if (o<c || o>n/2) return 0;
         if (o+c==n) return 1;
-        if (line.charAt(o+c)=='(') return p(o+1,c);
-        if (line.charAt(o+c)==')') return p(o,c+1);
-        if (line.charAt(o+c)=='.') return p(o+1,c) + p(o,c+1);
-        return 0;
+        if (a[o][c]==-1){
+            if (line.charAt(o+c)=='(') a[o][c] = p(o+1,c);
+            if (line.charAt(o+c)==')') a[o][c] = p(o,c+1);
+            if (line.charAt(o+c)=='.') a[o][c] = p(o+1,c) + p(o,c+1);
+        }
+        
+         return a[o][c];
     }
 }