标签:
Description
Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing that Ray has a photographic memory and this may not trouble him, Neal gives it to Jiejie.
Since Jiejie can‘t remember numbers clearly, he just uses sticks to help himself. Allowing for Jiejie‘s only 20071027 sticks, he can only record the remainders of the numbers divided by total amount of sticks.
The problem is as follows: a word needs to be divided into small pieces in such a way that each piece is from some given set of words. Given a word and the set of words, Jiejie should calculate the number of ways the given word can be divided, using the words in the set.
The input file contains multiple test cases. For each test case: the first line contains the given word whose length is no more than 300 000.
The second line contains an integer S<tex2html_verbatim_mark> , 1S4000<tex2html_verbatim_mark> .
Each of the following S<tex2html_verbatim_mark> lines contains one word from the set. Each word will be at most 100 characters long. There will be no two identical words and all letters in the words will be lowercase.
There is a blank line between consecutive test cases.
You should proceed to the end of file.
For each test case, output the number, as described above, from the task description modulo 20071027.
abcd 4 a b cd ab
Case 1: 2
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int ch[500010][32],val[500010]; int sz,d[500010]; int idx(char c) { return c-‘a‘; } void insert(char *s,int v) { int u=0,n=strlen(s); for(int i=0;i<n;i++) { int c=idx(s[i]); if(!ch[u][c]) { memset(ch[sz],0,sizeof(ch[sz])); val[sz]=0; ch[u][c]=sz++; } u=ch[u][c]; } val[u]=v; } int main() { char s[500010],a[105]; int i,x,t=1,j; while(~scanf("%s",s)) { memset(d,0,sizeof(d)); sz=1; memset(ch[0],0,sizeof(ch[0])); scanf("%d",&x); for(i=1;i<=x;i++) { scanf("%s",a); insert(a,-1); } int root,i,j,k; int len=strlen(s); d[len]=1; for(i=len-1;i>=0;i--) { root=0; for(j=0,k=i;k<len;j++,k++) { int c=idx(s[k]); if(ch[root][c]==0) break; else if(val[ch[root][c]]==-1) { d[i] += d[i+j+1]; if(d[i] >= 20071027) d[i] %= 20071027; } root=ch[root][c]; } } printf("Case %d: %d\n",t++,d[0]%20071027); } }
标签:
原文地址:http://www.cnblogs.com/cancangood/p/4333477.html