合法括号序列的前缀和均大于等于零,后缀和均小于等于0(左括号为1,右括号为-1)
string s;
map<int,int>qian,hou;
inline void solve()
{
int n;cin>>n;
int ans=0,mx=0;
rpp(i,n)
{
cin>>s;
mx=max(mx,sz(s));
int sum=0,flag=1;
for(auto x:s)
{
if(x=='(') ++sum;
else --sum;
if(sum<0) flag=0;
}
//枚举每个串作为后一个串
if(!flag)
{
int tmp=0,flag=1;
for(int i=sz(s)-1;i>=0;--i)
{
if(s[i]==')') --tmp;
else ++tmp;
if(tmp>0)
{
flag=0;
break;
}
}
if(flag) ++hou[-sum];
}
else if(sum==0) ++ans;
else ++qian[sum];
}
ans/=2;
rpp(i,mx+10) ans+=min(qian[i],hou[i]);
cout<<ans<<endl;
}