Guest User

solve

a guest
Apr 30th, 2014
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #pragma comment(linker, "/STACK:167177216")
  2.  
  3. #include <stdio.h>
  4. #include <stack>
  5. #include <math.h>
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <string.h>
  9. #include <string>
  10. #include <memory.h>
  11. #include <vector>
  12. #include <map>
  13. #include <queue>
  14. #include <set>
  15. #include <time.h>
  16. #include <cassert>
  17. using namespace std;
  18.  
  19. #define mp make_pair
  20. #define pb push_back
  21. #define pii pair<int, int>
  22. #define forn(i, n) for(int i = 0; i < (int)(n); i++)
  23. #define x first
  24. #define y second
  25.  
  26. typedef long long li;
  27. typedef long double ld;
  28. typedef unsigned long long uli;
  29.  
  30. const int INF = 1e9;
  31. const ld eps = 1e-9;
  32. const li mod = INF + 7;
  33. const li INF64 = (li)(INF) * (li)(INF);
  34.  
  35. const int ddx[] = {-1, 1, 1, -1};
  36. const int ddy[] = {1, 1, -1, -1};
  37. const int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
  38. const int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
  39. const int dx4[] = {-1, 0, 1, 0};
  40. const int dy4[] = {0, 1, 0, -1};
  41. const int dxh[] = {-1, -1, -1, 1, 1, 1, 1, -1};
  42. const int dyh[] = {1, -1, -1, -1, -1, 1, 1, 1};
  43. const string dirs[] = {"RIGHT", "UP", "LEFT", "DOWN"};
  44.  
  45. string s;
  46.  
  47. int toint(string s)
  48. {
  49.     int res = 0, st = 1;
  50.     for(int i = s.length() - 1; i >= 0; i--)
  51.     {
  52.         res += (s[i] - '0') * st;
  53.         st *= 10;
  54.     }
  55.  
  56.     return res;
  57. }
  58.  
  59. string calc(int l, int r)
  60. {
  61.     string res;
  62.     res.erase();
  63.  
  64.     int idx = l;
  65.     while(idx <= r)
  66.     {
  67.         while(idx <= r && s[idx] != '(')
  68.         {
  69.             res += s[idx];
  70.             idx++;
  71.         }
  72.  
  73.         if(idx > r)
  74.             break;
  75.  
  76.         idx++;
  77.         string val;
  78.         while(s[idx] >= '0' && s[idx] <= '9')
  79.         {
  80.             val += s[idx];
  81.             idx++;
  82.         }
  83.  
  84.         int nidx = idx;
  85.         int rr;
  86.         int kol = 1;
  87.         for(int i = idx; i <= r; i++)
  88.         {
  89.             if(s[i] == '(')
  90.                 kol++;
  91.             if(s[i] == ')')
  92.                 kol--;
  93.             if(kol == 0)
  94.             {
  95.                 rr = i;
  96.                 break;
  97.             }
  98.         }
  99.         //cout << "rr == " << rr << endl;
  100.         //while(s[idx] != ')')
  101.             //idx++;
  102.  
  103.         int cnt = toint(val);
  104.         forn(kk, cnt)
  105.             res += calc(nidx, rr - 1);
  106.         idx = rr + 1;
  107.     }
  108.  
  109.     return res;
  110. }
  111.  
  112. int main()
  113. {
  114.     //freopen("input.txt","r",stdin);
  115.     //freopen("output.txt","w",stdout);
  116.     ios_base::sync_with_stdio(false);
  117.    
  118.     getline(cin, s);
  119.     cout << calc(0, s.length() - 1) << endl;
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment