Wireless Password
Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u 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.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 2helloworld4 1 1icpc10 0 00 0 0Sample Output
2114195065
【题意】
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 #include<queue> 7 using namespace std; 8 #define Maxn 1010 9 #define Maxl 1010 10 #define INF 0xfffffff 11 #define Mod 20090717 12 13 int n,m,ak; 14 char s[210]; 15 16 struct node 17 { 18 int cnt,fail,mark; 19 int son[30]; 20 }t[Maxn];int tot;//=0; 21 int num; 22 int p[210]; 23 24 void upd(int x) 25 { 26 t[x].cnt=0;t[x].mark=0; 27 memset(t[x].son,0,sizeof(t[x].son)); 28 } 29 30 int mymin(int x,int y) {return x<y?x:y;} 31 32 void read_trie() 33 { 34 scanf("%s",s+1); 35 int len=strlen(s+1); 36 int now=0; 37 for(int i=1;i<=len;i++) 38 { 39 int ind=s[i]-'a'+1; 40 if(!t[now].son[ind]) 41 { 42 t[now].son[ind]=++tot; 43 upd(tot); 44 } 45 now=t[now].son[ind]; 46 if(i==len) 47 { 48 t[now].mark=1<<(++num)-1; 49 } 50 } 51 } 52 53 queue<int > q; 54 void build_AC() 55 { 56 while(!q.empty()) q.pop(); 57 q.push(0);//inq[0]=1; 58 while(!q.empty()) 59 { 60 int x=q.front();q.pop(); 61 for(int i=1;i<=26;i++) 62 { 63 if(t[x].son[i]) 64 { 65 t[t[x].son[i]].fail=x?t[t[x].fail].son[i]:0; 66 q.push(t[x].son[i]); 67 } 68 else t[x].son[i]=t[t[x].fail].son[i]; 69 t[x].mark|=t[t[x].fail].mark; 70 } 71 } 72 } 73 74 bool check(int x) 75 { 76 int h=0; 77 for(int i=1;i<=num;i++) if(x&(1<<i-1)) h++; 78 if(h>=ak) return 1; 79 return 0; 80 } 81 82 int f[30][260][2500]; 83 void dp() 84 { 85 for(int i=0;i<=n;i++) 86 for(int j=0;j<=tot;j++) 87 for(int l=0;l<=(1<<num)-1;l++) f[i][j][l]=0; 88 f[0][0][0]=1; 89 for(int i=1;i<=n;i++) 90 for(int j=0;j<=tot;j++) 91 for(int l=0;l<=(1<<num)-1;l++) if(f[i-1][j][l]!=0) 92 { 93 for(int k=1;k<=26;k++) 94 { 95 f[i][t[j].son[k]][l|t[t[j].son[k]].mark]=(f[i][t[j].son[k]][l|t[t[j].son[k]].mark]+f[i-1][j][l])%Mod; 96 } 97 } 98 int ans=0; 99 for(int i=0;i<=(1<<num)-1;i++) if(check(i)) 100 { 101 for(int j=0;j<=tot;j++) ans=(ans+f[n][j][i])%Mod; 102 } 103 printf("%d\n",ans); 104 } 105 106 void init() 107 { 108 tot=0;num=0; 109 upd(0); 110 for(int i=1;i<=m;i++) 111 { 112 read_trie(); 113 } 114 build_AC(); 115 } 116 117 int main() 118 { 119 int kase=0; 120 while(1) 121 { 122 scanf("%d%d%d",&n,&m,&ak); 123 if(n==0&&m==0&&ak==0) break; 124 init(); 125 dp(); 126 } 127 return 0; 128 }
2016-07-11 14:04:35