ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeforces Round #501 (Div. 3) F. Bracket Substring

2019-06-30 16:39:35  阅读:292  来源: 互联网

标签:const int ll Codeforces Substring Bracket maxn 1e9 include


 

https://codeforces.com/problemset/problem/1015/F

 

dp

'求包含某个子串的个数' 类型

 

kmp

    ///it is advised that all character begins at index 1

 

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <string>
 6 #include <algorithm>
 7 #include <iostream>
 8 using namespace std;
 9 #define ll long long
10 
11 const double eps=1e-8;
12 const ll inf=1e9;
13 const ll mod=1e9+7;
14 const int maxn=2e2+10;
15 
16 char sstr[maxn];
17 
18 int str[maxn],pre[maxn],nex[maxn][2];
19 ll f[maxn][maxn][maxn][2];
20 
21 int main()
22 {
23     int n,nn,len,i,j,k,l,m,jj,lll;
24     scanf("%d",&n);
25     nn=n<<1;
26     ///it is advised that all character begins at index 1
27     scanf("%s",sstr+1);
28     len=strlen(sstr+1);
29     for (i=1;i<=len;i++)
30         if (sstr[i]=='(')
31             str[i]=0;
32         else
33             str[i]=1;
34 
35     ///kmp makes O(1) visit
36     j=0;
37     pre[1]=0;
38     for (i=2;i<=len;i++)
39     {
40         while (j!=0 && str[j+1]!=str[i])
41             j=pre[j];
42         if (str[j+1]==str[i])
43             j++;
44         pre[i]=j;
45     }
46 
47     for (i=0;i<=len-1;i++)
48         for (j=0;j<=1;j++)
49             if (str[i+1]==j)
50                 nex[i][j]=i+1;
51             else
52                 nex[i][j]=nex[pre[i]][j];
53 
54     f[0][0][0][0]=1;
55     ///pos ith
56     for (i=1;i<=nn;i++)
57         ///j '('
58         for (j=0;j<=min(i-1,n);j++)
59             ///current char
60             for (k=0;k<=1;k++)
61             {
62                 jj=j+1+(-2)*(k==1);
63                 if (jj==-1)
64                     continue;
65                 (f[i][jj][0][1]+=f[i-1][j][0][1])%=mod;
66                 ///the lth str
67                 for (l=0;l<=min(len-1,i-1);l++)
68                 {
69                     lll=nex[l][k];
70                     if (lll==len)
71                         (f[i][jj][0][1]+=f[i-1][j][l][0])%=mod;
72                     else
73                         (f[i][jj][lll][0]+=f[i-1][j][l][0])%=mod;
74                 }
75             }
76     printf("%lld",f[nn][0][0][1]);
77     return 0;
78 }

 

标签:const,int,ll,Codeforces,Substring,Bracket,maxn,1e9,include
来源: https://www.cnblogs.com/cmyg/p/11110273.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有