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

 1 #include<bits/stdc++.h>
 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<<ans<<endl;
22   return 0;
23 }

 

posted @ 2022-01-09 20:22  matt-11  阅读(53)  评论(0)    收藏  举报