题目大意
对于任意长度n的小写字母字符串,不包含给定字符串个数。
解题思路
f[i][j]表示第i个字符,与给定串匹配了j位的方案数。预先用kmp预处理好填一个字符会匹配到哪里,dp转移即可。
code
#include<cstdio>
#include<cstring>
#include<algorithm>
#define min(a,b) ((a<b)?a:b)
#define fo(i,j,k) for(int i=j;i<=k;i++)
#define fd(i,j,k) for(int i=j;i>=k;i--)
using namespace std;
int const maxn=1e4,maxl=100;
int n,m,f[maxn+10][maxl+10],fail[maxl+10][26+10],mod=1e9+7;
char s[maxl+10];
int main(){
freopen("helloworld.in","r",stdin);
freopen("helloworld.out","w",stdout);
for(;scanf("%d",&n)!=EOF;){
scanf("%s",s+1);m=strlen(s+1);
int ok=1;
fo(i,1,m)if((s[i]<'a')||(s[i]>'z')){ok=0;break;}
if(!ok){
long long ans=1;
fo(i,1,n)ans=(ans*26)%mod;
printf("%lld\n",ans);
continue;
}
fo(j,0,25)fail[0][j]=fail[1][j]=0;
fo(i,2,m)
fo(j,0,25){
fail[i][j]=fail[i-1][s[i-1]-'a'];
for(;fail[i][j]&&(s[fail[i][j]+1]!='a'+j);fail[i][j]=fail[fail[i][j]][s[fail[i][j]]-'a']);
if(s[fail[i][j]+1]=='a'+j)fail[i][j]++;
}
fo(i,0,n)fo(j,0,m)f[i][j]=0;
f[0][0]=1;
fo(i,0,n-1)
fo(j,0,m-1)
if(f[i][j])fo(k,0,25){
if(k==s[j+1]-'a'){
if(j+1!=m)f[i+1][j+1]=(f[i+1][j+1]+f[i][j])%mod;
}else f[i+1][fail[j+1][k]]=(f[i+1][fail[j+1][k]]+f[i][j])%mod;
}
int ans=0;
fo(j,0,m-1)ans=(ans+f[n][j])%mod;
printf("%d\n",ans);
}
return 0;
}