Source code for submission s784

fq.cpp

  1. #include <algorithm>
  2. #include <cmath>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <iostream>
  6. #include <sstream>
  7. #include <map>
  8. #include <set>
  9. #include <queue>
  10. #include <vector>
  11. #include <cstring>
  12.  
  13. using namespace std;
  14.  
  15. #define FOR(prom, a, b) for(int prom = (a); prom < (b); prom++)
  16. #define FORD(prom, a, b) for(int prom = (a); prom > (b); prom--)
  17. #define FORDE(prom, a, b) for(int prom = (a); prom >= (b); prom--)
  18.  
  19. #define PB push_back
  20. #define MP make_pair
  21.  
  22. #define MM(co, cim) memset((co), (cim), sizeof((co)))
  23.  
  24. #define DEB(x) cerr << ">>> " << #x << " : " << x << endl;
  25.  
  26. int n, nh, dp[1014][1014];
  27. string line;
  28.  
  29. int go (int pos, int cnt)
  30. {
  31. if (cnt > nh) return 0;
  32. if (pos == n)
  33. {
  34. if (!cnt) return 1;
  35. else return 0;
  36. }
  37. if (dp[pos][cnt] != -1) return dp[pos][cnt];
  38. int res = 0;
  39. if (line[pos] == '.')
  40. {
  41. if (cnt > 0) res = (res + go(pos + 1, cnt - 1)) % 1000000;
  42. res = (res + go(pos + 1, cnt + 1)) % 1000000;
  43. }
  44. else
  45. {
  46. if (line[pos] == '(') res = (res + go(pos + 1, cnt + 1)) % 1000000;
  47. else
  48. {
  49. if (cnt > 0) res = (res + go(pos + 1, cnt - 1)) % 1000000;
  50. }
  51. }
  52. //printf("dp[%d][%d] = %d\n", pos, cnt, res);
  53. return dp[pos][cnt] = res;
  54. }
  55.  
  56. int main ()
  57. {
  58. while (getline(cin, line))
  59. {
  60. n = line.length();
  61. nh = n / 2;
  62. FOR(i, 0, 1014) MM(dp[i], -1);
  63. printf("%d\n", go(0, 0));
  64. }
  65. return 0;
  66. }
  67.