Description
給你一條由小寫字母組成的長字符串
如:
多組測試數據。
Input
每組測試數據:
第一行:一個字符串,
第二行:一個整數,
接下來
Output
若干行:
每行格式爲:”
Solution
看到方案數就可以想到用
Fi =sum{Fi−len(Cj) |Cj=Fi−lenCJ...i }
顯然會超時,每次搜索一遍
這就要用到
把
因爲
Code
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int maxL=300100;
const int maxl=110;
const int maxn=4010;
char str1[maxL],word[maxn][maxl];
int trie[maxn*maxl][30],sz,check[maxn*maxl],q,L;
int n;
long long int f[maxL];
int T(char a)
{return int(a-'a');}
void maketrie(int x)
{
int u=0,N=strlen(word[x]);
for(int i=0;i<N;i++)
{
if(!trie[u][T(word[x][i])])
{
memset(trie[++sz],0,sizeof(trie[sz]));
check[sz]=0;
trie[u][T(word[x][i])]=sz;
}
u=trie[u][T(word[x][i])];
}
check[u]++;
}
void find(int x)
{
int u=0;
for(int i=1;trie[u][T(str1[x])]&&x<L;i++,x++)
{
u=trie[u][T(str1[x])];
if(check[u])
f[x+1]=(f[x+1]+f[x+1-i]*check[u])%20071027;
}
}
void init()
{
sz=0;
memset(f,0,sizeof(f));
memset(check,0,sizeof(check));
memset(trie[0],0,sizeof(trie[0]));
f[0]=1;
q++;
}
int main()
{
while(scanf("%s",str1)==1)
{
init();
L=strlen(str1);
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%s",word[i]);
for(int i=1;i<=n;i++) maketrie(i);
for(int i=0;i<L;i++) find(i);
printf("Case %d: %lld\n",q,f[L]);
}
return 0;
}