Source code for submission s623

fq.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. char iq[2000];
  7. int ql;
  8.  
  9. int mem[1500][1500];
  10.  
  11. int count(int left5, int left10)
  12. {
  13. int result, pos;
  14. if (left5 < 0 || left10 < 0) return 0;
  15. if (left5 == 0 && left10 == 0) return 1;
  16. if (left10 < left5) return 0;
  17.  
  18. if (mem[left5][left10] >= 0) return mem[left5][left10];
  19.  
  20. pos = ql - left5 - left10;
  21. if (iq[pos] == '(') {
  22. result = count(left5-1, left10);
  23. } else if (iq[pos] == ')') {
  24. result = count(left5, left10-1);
  25. } else {
  26. result = (count(left5-1, left10) + count(left5, left10-1)) % 1000000;
  27. }
  28. //printf("pos %d l5 %d l10 %d ch %c ret %d\n", pos, left5, left10, iq[pos], result);
  29. mem[left5][left10] = result;
  30. return result;
  31. }
  32.  
  33. int main(int argc, char **argv)
  34. {
  35. int rv;
  36. while (1) {
  37. if (!gets(iq)) break;
  38. ql = strlen(iq);
  39. memset(mem, -1, sizeof(mem));
  40. printf("%d\n", count(ql/2, ql/2));
  41. }
  42. return 0;
  43. }
  44.