这个dp可以说很暴力了。。自己想得太复杂了。。
设个 d[i][j][k],j为平衡系数(其实就是左括号-右括号。。以后就这么叫了),i个字符,匹配到s的第k个字符的方案数。。
然后转移的时候k==m的情况是不参与转移的,排除掉了子串包含 s的情况。。然后用平衡系数==0的所有情况减掉k<m的情况就可以了。。
然后是关于下一位不与s匹配k应该跳到哪的问题。。这个可以暴力取子串比对。。应该也可以kmp?
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━━━━━━━━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 205
#define nm 100005
#define N 1000005
#define M(x,y) x=max(x,y)
const double pi=acos(-1);
const ll inf=1e9+7;
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
int n,m,c[NM][2];
ll d[NM][NM][NM],f[NM][NM],ans;
char s[NM];
int main(){
n=read()*2;scanf("%s",s+1);m=strlen(s+1);
if(s[1]=='(')c[0][1]++;else c[0][0]++;
inc(i,1,m-1)if(s[i+1]=='('){
c[i][1]=i+1;
dec(j,i,1)if(s[j]==')'){
bool f=true;
inc(k,1,j-1)if(s[k]!=s[k+i-j+1]){f=false;break;}
if(f){c[i][0]=j;break;}
}
}else{
c[i][0]=i+1;
dec(j,i,1)if(s[j]=='('){
bool f=true;
inc(k,1,j-1)if(s[k]!=s[k+i-j+1]){f=false;break;}
if(f){c[i][1]=j;break;}
}
}
d[0][0][0]=1;
inc(i,1,n){
inc(j,0,n)inc(k,0,m-1)if(d[i-1][j][k]){
(d[i][j+1][c[k][1]]+=d[i-1][j][k])%=inf;
if(j)(d[i][j-1][c[k][0]]+=d[i-1][j][k])%=inf;
//printf("%d %d %d\n",i-1,j,k);
}
}
f[0][0]=1;
inc(i,1,n)inc(j,0,n){
(f[i][j+1]+=f[i-1][j])%=inf;
if(j)(f[i][j-1]+=f[i-1][j])%=inf;
}
ans=f[n][0];
inc(i,0,m-1)ans=(ans-d[n][0][i]+inf)%inf;
return 0*printf("%I64d\n",ans);
}
F. Bracket Substring
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given a bracket sequence s
(not necessarily a regular one). A bracket sequence is a string containing only characters '(' and ')'.
A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not.
Your problem is to calculate the number of regular bracket sequences of length 2n
containing the given bracket sequence s as a substring (consecutive sequence of characters) modulo 109+7 (1000000007
).
Input
The first line of the input contains one integer n
(1≤n≤100) — the half-length of the resulting regular bracket sequences (the resulting sequences must have length equal to 2n
).
The second line of the input contains one string s
(1≤|s|≤200) — the string s that should be a substring in each of the resulting regular bracket sequences (|s| is the length of s
).
Output
Print only one integer — the number of regular bracket sequences containing the given bracket sequence s
as a substring. Since this number can be huge, print it modulo 109+7 (1000000007
).
Examples
Input
Copy
5 ()))()
Output
Copy
5
Input
Copy
3 (()
Output
Copy
4
Input
Copy
2 (((
Output
Copy
0
Note
All regular bracket sequences satisfying the conditions above for the first example:
- "(((()))())";
- "((()()))()";
- "((()))()()";
- "(()(()))()";
- "()((()))()".
All regular bracket sequences satisfying the conditions above for the second example:
- "((()))";
- "(()())";
- "(())()";
- "()(())".
And there is no regular bracket sequences of length 4
containing "(((" as a substring in the third example.