[DP]JZOJ 5804 简单的序列
分析
设一个DP为f[i][j]表示q串长度为i时,左右括号之和(一个1一个-1)为j时的方案数,显然最终答案为f[i][j]*f[n-m-i][j+lenofs]的和
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#pragma GCC optimize(2) #include <iostream> #include <cstdio> #include <cstring> using namespace std; const long long P=1e9+7; int n,m; char c[100001]; int s[100001],mins; long long f[2001][2002]; int main() { freopen("bracket.in","r",stdin); freopen("bracket.out","w",stdout); scanf("%d%d",&n,&m); scanf("%s",c); int len=strlen(c);mins=2147483647; for (int i=1;i<=len;i++) { s[i]=s[i-1]+(c[i-1]=='('?1:-1); mins=min(mins,s[i]); } f[0][0]=1; for (int i=1;i<=n-m;i++) for (int j=0;j<=i;j++) { if (j) f[i][j]+=f[i-1][j-1]; f[i][j]%=P; f[i][j]+=f[i-1][j+1]; f[i][j]%=P; } long long ans=0; for (int i=0;i<=n-m;i++) for (int j=0;j<=i;j++) if (j+s[len]<=n-m&&j+mins>=0) ans=(ans+f[i][j]*f[n-m-i][j+s[len]]%P)%P; printf("%lld",ans); fclose(stdin);fclose(stdout); }
在日渐沉没的世界里,我发现了你。