hdu 2825ac自动机+dp

xiaoxiao2021-02-28  79

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 题意: 给 m 个单词构成的集合,统计所有长度为 n 的串中,包含至少 k 个单词的方案数。 思路:建立ac自动机就不用说了,说一下dp,由于单词的个数比较少,所以我们可以用二进制进行状压 dp[i][j][k], i表示串的长度 j表示的是当前跑到ac自动机的节点号 k表示的是当前的状态(已经存在的单词的二进制表示) ac代码: #include<cstdio> #include<cstring> #include<queue> using namespace std; int tn,ch[111][26],fail[111],flag[111]; void insert(char *s,int k){ int x=0; for(int i=0; s[i]; ++i){ int y=s[i]-'a'; if(ch[x][y]==0) ch[x][y]=++tn; x=ch[x][y]; } flag[x]|=1<<k; } void init(){ memset(fail,0,sizeof(fail)); queue<int> que; for(int i=0; i<26; ++i){ if(ch[0][i]) que.push(ch[0][i]); } while(!que.empty()){ int x=que.front(); que.pop(); for(int i=0; i<26; ++i){ if(ch[x][i]) que.push(ch[x][i]),fail[ch[x][i]]=ch[fail[x]][i],flag[ch[x][i]]|=flag[ch[fail[x]][i]]; else ch[x][i]=ch[fail[x]][i]; } } } int getCnt(int s){ int res=0; for(int i=0; i<10; ++i){ if((s>>i)&1) ++res; } return res; } int d[26][111][1<<10]; int main(){ char str[11]; int n,m,k; while(~scanf("%d%d%d",&n,&m,&k) && (n||m||k)){ tn=0; memset(ch,0,sizeof(ch)); memset(flag,0,sizeof(flag)); for(int i=0; i<m; ++i){ scanf("%s",str); insert(str,i); } init(); memset(d,0,sizeof(d)); d[0][0][0]=1; for(int i=0; i<n; ++i){ for(int j=0; j<=tn; ++j){ for(int k=0; k<(1<<m); ++k){ if(d[i][j][k]==0) continue; for(int y=0; y<26; ++y){ d[i+1][ch[j][y]][k|flag[ch[j][y]]]+=d[i][j][k]; d[i+1][ch[j][y]][k|flag[ch[j][y]]]%=20090717; } } } } int res=0; for(int i=0; i<=tn; ++i){ for(int j=0; j<(1<<m); ++j){ if(getCnt(j)<k) continue; res+=d[n][i][j]; res%=20090717; } } printf("%d\n",res); } return 0; }
转载请注明原文地址: https://www.6miu.com/read-85404.html

最新回复(0)