Longest Regular Bracket Sequence
题面翻译
给出一个括号序列,求出最长合法子串和它的数量。
合法的定义:这个序列中左右括号匹配
题目描述
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
输入格式
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed $ 10^{6} $ .
输出格式
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing “0 1”.
样例 #1
样例输入 #1
)((())))(()())
样例输出 #1
6 2
样例 #2
样例输入 #2
))(
样例输出 #2
0 1
思路
这种题一看是括号有关的题,跟括号有关的题叫做括号序列,对于括号序列的题我们可以用动态规划+栈来做。
具体做法:
状态表示:f[i]表示以i结尾的最长合法序列的方案数
。
状态转移:f[i]=i-j+1+f[j-1](其中j是上一次匹配的结尾)
。
上一次匹配的结尾我们就得用栈来维护了。
但题目到这里还没有结束,题目还要记录下标,因此我们就想到类似桶排序的方式。
特别注意:此时桶排序中括号内(也就是它的下标表示的是i结尾的最长合法序列的方案数(也就是方案数)),桶排序的值指的是这种相同序列的个数。
参考:
代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<stack>
using namespace std;
const int N = 1e6+10;
int f[N];
char s[N];
int t[N];
stack<int>stk;//里面我直接放下标多好
int n;
int main(){
cin>>s+1;
n=strlen(s+1);
for(int i=1;i<=n;i++){
if(s[i]=='(')stk.push(i);
else if(!stk.empty()){
int j=stk.top();
t[f[i]=i-j+1+f[j-1]]++;
stk.pop();
}
}
//倒着找
for(int i=n;i>=0;i--){
if(t[i]){
cout<<i<<' '<<t[i];
return 0;
}
}
cout<<"0 1";
return 0;
}