右括号匹配不到答案加一,左括号多2个答案加一。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int n,now=0,ans=0;char s[100005];
scanf("%s",s+1);
n=strlen(s+1);
for(int i=1;i<=n;i++)
{
if(s[i]=='(')
now++;
else
{
if(now==0)
ans++,now++;
else now--;
}
}
ans+=now/2+(now%2);
cout<<ans;
}