每个状态结束时,要及时清零标识变量和累积变量。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<list>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define memset(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
char com;
int main()
{
int length;
int inside_number;
int temp_number;
int outside_length;
int temp_length;
int open=0;
int inside_flag=0;
while(~scanf("%d",&length)){
inside_number=0;
temp_number=0;
outside_length=0;
temp_length=0;
open=0;
inside_flag=0;
//cout<<"eee"<<endl;
char tt=getchar();
//char gg=getchar();
//cout<<"length: "<<length<<endl;
for(int i=1;i<=length;i++){
//cout<<i<<endl;
scanf("%c",&com);
if(com=='\n'){
continue;
}
//cout<<com<<endl;
//Cal
if(open==0){
if(com=='('){
open=1;
outside_length=max(outside_length,temp_length);
temp_length=0;//每个状态的累计变量和标志标量,在状态结束时,一定要及时清零!!!
}
else if(com=='_'){
outside_length=max(outside_length,temp_length);
temp_length=0;
}
else{
temp_length++;
}
}
else if(open==1){
if(com==')'){
if(inside_flag!=0){
temp_number++;
}
inside_flag=0;
open=0;
//inside_number+=temp_number;
//inside_number=max(inside_number,temp_number);
}
else if(com=='_'){
if(inside_flag!=0){
temp_number++;
}
inside_flag=0;
}
else{
inside_flag=1;
}
}
}
outside_length=max(outside_length,temp_length);
//cout<<"hehe"<<endl;
cout<<outside_length<<" "<<temp_number<<endl;
}
}