hdu2825Wireless Password

地址:http://acm.hdu.edu.cn/showproblem.php?pid=2825

题目:

Wireless Password

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6862    Accepted Submission(s): 2279


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.
 
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
 
 
思路:
  dp[i][j]表示长度为i的串走到j节点时的答案的数量。
  1 #include <queue>
  2 #include <cstring>
  3 #include <cstdio>
  4 using namespace std;
  5 
  6 const int INF=0x3f3f3f3f;
  7 struct AC_auto
  8 {
  9     const static int LetterSize = 26;
 10     const static int TrieSize = LetterSize * ( 1e3 + 50);
 11 
 12     int tot,root,fail[TrieSize],end[TrieSize],next[TrieSize][LetterSize];
 13     int dp[30][150][1<<10];
 14     int newnode(void)
 15     {
 16         memset(next[tot],-1,sizeof(next[tot]));
 17         end[tot] = 0;
 18         return tot++;
 19     }
 20 
 21     void init(void)
 22     {
 23         tot = 0;
 24         root = newnode();
 25     }
 26 
 27     int getidx(char x)
 28     {
 29         return x-'a';
 30     }
 31 
 32     void insert(char *ss,int x)
 33     {
 34         int len = strlen(ss);
 35         int now = root;
 36         for(int i = 0; i < len; i++)
 37         {
 38             int idx = getidx(ss[i]);
 39             if(next[now][idx] == -1)
 40                 next[now][idx] = newnode();
 41             now = next[now][idx];
 42         }
 43         end[now]|=x;
 44     }
 45 
 46     void build(void)
 47     {
 48         queue<int>Q;
 49         fail[root] = root;
 50         for(int i = 0; i < LetterSize; i++)
 51             if(next[root][i] == -1)
 52                 next[root][i] = root;
 53             else
 54                 fail[next[root][i]] = root,Q.push(next[root][i]);
 55         while(Q.size())
 56         {
 57             int now = Q.front();Q.pop();
 58             for(int i = 0; i < LetterSize; i++)
 59             if(next[now][i] == -1)   next[now][i] = next[fail[now]][i];
 60             else
 61             {
 62                  fail[next[now][i]] = next[fail[now]][i];
 63                  end[next[now][i]]|=end[fail[next[now][i]]];
 64                  Q.push(next[now][i]);
 65             }
 66         }
 67     }
 68 
 69     int match(char *ss)
 70     {
 71         int len,now,res;
 72         len = strlen(ss),now = root,res = 0;
 73         for(int i = 0; i < len; i++)
 74         {
 75             int idx = getidx(ss[i]);
 76             int tmp = now = next[now][idx];
 77             while(tmp)
 78             {
 79                 res += end[tmp];
 80                 end[tmp] = 0;//按题目修改
 81                 tmp = fail[tmp];
 82             }
 83         }
 84         return res;
 85     }
 86 
 87     int go(int n,int m,int kd)
 88     {
 89         int ans=0;
 90         memset(dp,0,sizeof dp);
 91         dp[0][0][0]=1;
 92         for(int i=0,mx=1<<m;i<n;i++)
 93         for(int j=0;j<tot;j++)
 94         for(int k=0;k<mx;k++)
 95         for(int p=0;p<LetterSize&&dp[i][j][k];p++)
 96         {
 97             dp[i+1][next[j][p]][k|end[next[j][p]]]+=dp[i][j][k];
 98             if(dp[i+1][next[j][p]][k|end[next[j][p]]]>=20090717)
 99                 dp[i+1][next[j][p]][k|end[next[j][p]]]-=20090717;
100         }
101         for(int i=0,mx=1<<m;i<tot;i++)
102         for(int j=0;j<mx;j++)
103         {
104             int cnt=0;
105             for(int k=0;k<m;k++)
106             if(j&(1<<k))
107                 cnt++;
108             if(cnt>=kd) ans+=dp[n][i][j];
109             if(ans>=20090717) ans-=20090717;
110         }
111         return ans;
112     }
113     void debug()
114     {
115         for(int i = 0;i < tot;i++)
116         {
117             printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]);
118             for(int j = 0;j < LetterSize;j++)
119                 printf("%3d",next[i][j]);
120             printf("]
");
121         }
122     }
123 };
124 AC_auto ac;
125 char ss[2000];
126 int main(void)
127 {
128     int n,k,m;
129     while(~scanf("%d%d%d",&n,&m,&k)&&(m||n||k))
130     {
131         ac.init();
132         for(int i=0;i<m;i++)
133             scanf("%s",ss),ac.insert(ss,1<<i);
134         ac.build();
135         printf("%d
",ac.go(n,m,k));
136     }
137     return 0;
138 }
 
原文地址:https://www.cnblogs.com/weeping/p/7447020.html