Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. Nurzhan Yergozhin (guess)
  2. Submitted: Sat, Nov 17, 2012 10:41:58 EST
  3.  
  4.  /*
  5.  LANG : C++
  6.  PROG : typo
  7.  */
  8.  
  9.  # include <iostream>
  10.  # include <cstring>
  11.  # include <algorithm>
  12.  # include <cstdio>
  13.  # include <cstdlib>
  14.  # include <stack>
  15.  
  16.  using namespace std;
  17.  
  18.  const int N = 1000000;
  19.  
  20.  string s;
  21.  
  22.  int l[N][3], r[N][3], len, ans, ul[N][3], ur[N][3];
  23.    
  24.  stack <int> st;                            
  25.  
  26.  int main () {
  27.  
  28.     freopen("typo.in", "r", stdin);
  29.     freopen("typo.out", "w", stdout);
  30.  
  31.     cin >> s;
  32.     len = s.size();
  33.    
  34.     for (int i = 0; i < len; ++i) {            
  35.  
  36.         if(s[i] == '(') st.push(i);
  37.         else if(st.size() > 0 && s[i] == ')') st.pop();
  38.         else if(st.size() == 0 && s[i] == ')') {
  39.             l[i][2] ++; ul[i][2] = 1;
  40.         }
  41.         l[i][1] = st.size();
  42.         l[i][2] += (i ? l[i - 1][2] : 0);
  43.     }
  44.                  
  45.     while(!st.empty())  st.pop();  
  46.     for (int j = len - 1; j >= 0; --j) {
  47.         if(s[j] == ')') st.push(j);
  48.         else if(st.size() > 0 && s[j] == '(') st.pop();
  49.         else if(st.size() == 0 && s[j] == '(') {
  50.             r[j][2] ++; ur[j][2] = 1;
  51.         }
  52.         r[j][1] = st.size();
  53.         r[j][2] += r[j + 1][2];
  54.     }
  55.    
  56.  
  57.     for (int i = 0; i < len; ++i) {
  58.                          
  59.     //  cout << l[i][1] << ' ' << l[i][2] << ' ' << r[i][1] << ' ' << r[i][2] << endl;          
  60.         if(s[i] == '(') {
  61.             int cntl = (i ? l[i - 1][1] : 0);
  62.             int cnt2 = (i ? l[i - 1][2] : 0);
  63.             if(cntl == r[i + 1][1] + 1 && r[i + 1][2] == 0 && cnt2 == 0)
  64.                 ans ++;        
  65.                
  66.         }
  67.  
  68.         if(s[i] == ')') {  
  69.             int cnt1, cnt2;
  70.             if(i == 0) cnt1 = 1;
  71.             else if(ul[i][2] == 1) cnt1 = l[i][1] + 1;
  72.             else cnt1 = l[i][1] + 2;                  
  73.  
  74.             if(l[i][2] != 1 && ul[i][2]) continue;
  75.             if(l[i][2] != 0 && !ul[i][2]) continue;
  76.             if(r[i + 1][2] != 0) continue;
  77.  
  78.             if(cnt1 == r[i + 1][1])
  79.                 ans ++;          
  80.        
  81.         }
  82.  
  83.     }
  84.    
  85.     cout  << ans << endl;      
  86.                                      
  87.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement