51nod1478(yy)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1478&judgeId=365133
题意: 中文题诶~
思路: 令左括号的值为 1, 右括号的值为 -1, 然后再用前缀i和搞一下就好了.
代码:
1 #include <iostream> 2 using namespace std; 3 4 const int MAXN = 1e6 + 10; 5 int vis[MAXN], tag[MAXN]; 6 7 int main(void){ 8 string s; 9 cin >> s; 10 for(int i = 0; i < s.size(); i++){ 11 if(s[i] == '(') vis[i] = 1; 12 else vis[i] = -1; 13 } 14 int sol = 0, i = 0; 15 while(i < s.size()){ 16 int cnt = 0; 17 for(int j = i; j < s.size(); j++){ 18 cnt += vis[j]; 19 if(cnt == 0){ 20 sol = max(sol, j - i + 1); 21 tag[j - i + 1]++; 22 }else if(cnt < 0){ 23 i = j; 24 break; 25 } 26 } 27 i++; 28 } 29 cout << sol << " " << (tag[sol] == 0 ? 1 : tag[sol]) << endl; 30 return 0; 31 }
我就是我,颜色不一样的烟火 --- geloutingyu