本文主要是介绍CF 5 C. Longest Regular Bracket Sequence,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:Longest Regular Bracket Sequence
思路:简单dp
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <stack>
using namespace std;
#define maxn 1000010
int dp[maxn];
stack<int>st;
int main()
{while(!st.empty())st.pop();string s;cin>>s;for(int i=0;i<s.size();i++){if(s[i]=='(')st.push(i);else{if(!st.empty()){int tmp=st.top();st.pop();if(tmp)dp[i]=dp[tmp-1]+i-tmp+1;elsedp[i]=dp[0]+i-tmp+1;}}}int ans=0,cnt=1;for(int i=0;i<s.size();i++){if(dp[i]>ans){cnt=1;ans=dp[i];}else if(dp[i]==ans)cnt++;}if(ans==0)cout<<"0 1"<<endl;elsecout<<ans<<" "<<cnt<<endl;return 0;
}
这篇关于CF 5 C. Longest Regular Bracket Sequence的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!