ACWing 4207. 最长合法括号子序列


 1 #include
 2 using namespace std;
 3 
 4 int main()
 5 {
 6   string s;
 7   cin>>s;
 8   int ans=0,cnt=0;
 9   for(auto &p:s)
10   {
11     if(p=='(')cnt++;
12     else
13     {
14       if(cnt)
15       {
16         cnt--;
17         ans+=2;
18       }
19     }
20   }
21   cout<endl;
22   return 0;
23 }

相关