Wireless Password
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7855 Accepted Submission(s): 2560
Problem Description
Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless network in the building. Liyuan did not know the password of the network, but he got some important information from his neighbor. He knew the password consists only of lowercase letters 'a'-'z', and he knew the length of the password. Furthermore, he got a magic word set, and his neighbor told him that the password included at least k words of the magic word set (the k words in the password possibly overlapping).
For instance, say that you know that the password is 3 characters long, and the magic word set includes 'she' and 'he'. Then the possible password is only 'she'.
Liyuan wants to know whether the information is enough to reduce the number of possible passwords. To answer this, please help him write a program that determines the number of possible passwords.
For instance, say that you know that the password is 3 characters long, and the magic word set includes 'she' and 'he'. Then the possible password is only 'she'.
Liyuan wants to know whether the information is enough to reduce the number of possible passwords. To answer this, please help him write a program that determines the number of possible passwords.
Input
There will be several data sets. Each data set will begin with a line with three integers n m k. n is the length of the password (1<=n<=25), m is the number of the words in the magic word set(0<=m<=10), and the number k denotes that the password included at least k words of the magic set. This is followed by m lines, each containing a word of the magic set, each word consists of between 1 and 10 lowercase letters 'a'-'z'. End of input will be marked by a line with n=0 m=0 k=0, which should not be processed.
Output
For each test case, please output the number of possible passwords MOD 20090717.
Sample Input
10 2 2
hello
world
4 1 1
icpc
10 0 0
0 0 0
Sample Output
2
1
14195065
Source
f[i][j][k]表示当前长度为i,在树上的j节点,包含字符状况为k的方案个数,状态转移时选择26个字母中的一个进行转移即可。
卡时间,少用memset() = =
1 #include<iostream> 2 #include<cstring> 3 #include<queue> 4 #include<cstdio> 5 #include<stack> 6 #include<set> 7 #include<map> 8 #include<cmath> 9 #include<ctime> 10 #include<time.h> 11 #include<algorithm> 12 using namespace std; 13 #define mp make_pair 14 #define debug puts("debug") 15 #define LL long long 16 #define pii pair<int,int> 17 const int MOD=20090717; 18 int N,M,K; 19 struct ach{ 20 int f[30][120][(1<<11)]; 21 int ch[120][26],fail[120]; 22 int tot,end[120]; 23 24 void init(){ 25 tot=0; 26 newnode(); 27 } 28 29 int newnode(){ 30 memset(ch[tot],-1,sizeof(ch[tot])); 31 end[tot]=0; 32 fail[tot]=0; 33 return tot++; 34 } 35 void insert(char *s,int x){ 36 int n=strlen(s),u=0; 37 for(int i=0;i<n;++i){ 38 int c=s[i]-'a'; 39 if(ch[u][c]==-1) ch[u][c]=newnode(); 40 u=ch[u][c]; 41 } 42 end[u]|=(1<<x); 43 } 44 45 void build_fail(){ 46 queue<int>q; 47 fail[0]=0; 48 for(int i=0;i<26;++i){ 49 if(ch[0][i]==-1) ch[0][i]=0; 50 else{ 51 fail[ch[0][i]]=0; 52 q.push(ch[0][i]); 53 } 54 } 55 56 while(!q.empty()){ 57 int u=q.front(); 58 q.pop(); 59 end[u]|=end[fail[u]]; 60 for(int i=0;i<26;++i){ 61 if(ch[u][i]==-1){ 62 ch[u][i]=ch[fail[u]][i]; 63 } 64 else{ 65 fail[ch[u][i]]=ch[fail[u]][i]; 66 q.push(ch[u][i]); 67 } 68 } 69 } 70 } 71 72 int cal(int x){ 73 int r=0; 74 while(x){ 75 r+=(x&1); 76 x>>=1; 77 } 78 return r; 79 } 80 81 void solve(){ 82 int ALL=(1<<M); 83 for(int i=0;i<=N;++i){ 84 for(int j=0;j<tot;++j){ 85 for(int k=0;k<ALL;++k){ 86 f[i][j][k]=0; 87 } 88 } 89 } 90 f[0][0][0]=1; 91 int i,j,k,ans=0; 92 for(i=0;i<=N;++i){ 93 for(j=0;j<tot;++j){ 94 for(k=0;k<ALL;++k){ 95 if(f[i][j][k]){ 96 for(int p=0;p<26;++p){ 97 int ni=i+1; 98 int nj=ch[j][p]; 99 int nk=k|end[nj]; 100 101 f[ni][nj][nk]+=f[i][j][k]; 102 f[ni][nj][nk]%=MOD; 103 } 104 } 105 } 106 } 107 } 108 109 for(k=0;k<ALL;++k){ 110 if(cal(k)>=K){ 111 for(j=0;j<tot;++j) 112 ans+=f[N][j][k]; 113 ans%=MOD; 114 } 115 } 116 117 cout<<ans<<endl; 118 } 119 }a; 120 int main(){ 121 122 while(cin>>N>>M>>K&&(N||M||K)){ 123 a.init(); 124 char s[15]; 125 for(int i=0;i<M;++i){ 126 scanf("%s",s); 127 a.insert(s,i); 128 } 129 a.build_fail(); 130 a.solve(); 131 } 132 return 0; 133 }