http://codeforces.com/problemset/problem/723/B
求括号内单词数和括号外最大单词长度,注意细心,尤其是ok和sum的置0。
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<cmath> #define INF 0x3f3f3f3f using namespace std; int main() { int n; char s[300]; scanf("%d",&n); getchar(); gets(s); int maxx = 0,cnt = 0,flag = 0,ok = 0,sum = 0; for(int i = 0;i < n;i++) { if(s[i] == '(') { flag = 1; ok = 0; } else if(s[i] == ')') { sum = 0; flag = 0; if(ok) cnt++; } else if(flag == 0) { if(s[i] == '_') sum = 0; else { sum++; maxx = max(sum,maxx); } } else if(flag == 1) { if(s[i] == '_') { if(ok) cnt++; ok = 0; } else ok = 1; } } printf("%d %d\n",maxx,cnt); return 0; }