cf5c

题目链接:http://www.codeforces.com/problemset/problem/5/C

思路:如果str[i]=='(',那么入栈;否则如果str[i]==')',我们用一个p来保存i的位置;否则就是str[i]==')'的情况了,首先当然是出栈了,然后就是求更新。

具体就代码:

View Code
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<stack>
 5 using namespace std;
 6 #define MAXN 1000000+10
 7 char str[MAXN];
 8 
 9 int main(){
10     while(~scanf("%s",str)){
11         int ans=0,count=0,p=-1,len=strlen(str),tmp;
12         stack<int>S;
13         for(int i=0;i<len;i++){
14             if(str[i]=='(')S.push(i);
15             else if(S.empty())p=i;
16             else {
17                 S.pop();
18                 S.empty()?tmp=i-p:tmp=i-S.top();
19                 if(tmp>ans)ans=tmp,count=1;
20                 else if(tmp==ans)count++;
21             }
22         }
23         if(ans==0)count=1;
24         printf("%d %d\n",ans,count);
25     }
26     return 0;
27 }
28 
29 
30 
31 
32     
33     
原文地址:https://www.cnblogs.com/wally/p/3058566.html