上代码
#include<bits/stdc++.h>
using namespace std;
char c;
int d[1009],ans=0,cnt,okk=0,z[1009],mx=INT_MIN,s[1009];
int main()
{
cnt=0;
while(cin>>c)
{
cnt++;
if(c=='(')d[cnt]=-1;
if(c==')')d[cnt]=1;
s[cnt]=s[cnt-1]+d[cnt];
}
if(cnt%2==1)
{
cout<<"0 1";
return 0;
}
for(int i=1;i<=cnt;i++)
{
for(int j=i+1;j<=cnt;j+=2)
{
if(s[j]-s[i-1]==0)
{
int top=0;
int ok=1;
for(int x=i;x<=j;x++)
{
if(d[x]<0)
{
top++;
z[top]=d[x];
}
else
{
if(top==0)
{
ok=0;
break;
}
if(d[x]+z[top]!=0)
{
ok=0;
break;
}
top--;
}
}
if(ok==1)
{
okk=1;
if(j-i+1>mx)
{
mx=j-i+1;
ans=1;
}
else
{
if(j-i+1==mx)
{
ans++;
}
}
}
}
}
}
if(okk==0)cout<<"0 1";
else cout<<mx<<" "<<ans;
}
我的思路是先用前缀和和cnt判断出哪些可能是对的,再在可能是对的中判断哪些是对的
int top=0;
int ok=1;
for(int x=i;x<=j;x++)
{
if(d[x]<0)
{
top++;
z[top]=d[x];
}
else
{
if(top==0)
{
ok=0;
break;
}
if(d[x]+z[top]!=0)
{
ok=0;
break;
}
top--;
}
}
上面是判断是否合法的代码
有没有dalao帮我看看哪错了