hdu2825Wireless Password【ac自动机+dp状态压缩】
来源:互联网 发布:mac safari 缓存 编辑:程序博客网 时间:2024/05/16 12:00
Total Submission(s): 5502 Accepted Submission(s): 1737
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 2hello world 4 1 1icpc 10 0 00 0 0
Sample Output
2114195065
Source
2009 Multi-University Training Contest 1 - Host by TJU
Recommend
gaojie | We have carefully selected several similar problems for you: 2819 2824 2817 2823 2822
扔了大半年的Ac自动机全还回去了,特别不理解先做第一题的人的心理,第一题还得状态压缩,这题就不用压啊
说题意:邻居wifi密码由n个字符组成,由m个子字符串的至少k个组成,问有多少种排列组合的方法。首先复习了一下,AC自动机,next[L][26] fail[L] end[L]分别表示下一个节点的指针,失败指针类似于kmp的next指针,计数的数组(这道题里这个数组不是直接++的,下文会说)
一共有init() insert() build() solve()这些函数,除了最后一个,都是Ac自动机上的模板,大体结构不会变==最后一个函数之前是用来求个数的,其实这次也是,但是需要用dp==
dp[i][j][k]表示长度为i匹配到状态j 各个word出现的情况为k的方法数,解释一下什么意思~i,没得说 1~len;j是状态0~L;k要好好说一下,这个题还是用状压了的,它需要加一个数组num[],二进制中的每一位表示某个单词是否出现过,最最开始的时候初始化使得num[i]储存的值是i中二进制下1的个数,即所包含的单词个数,这个i也就是dp的最后一维。再说说原本用来计数的数组,原本是可以直接++计数的,然而由于状态是相互转化的,所以所有涉及到的地方都是按位或~原本的统计函数是模式串的个数的,从头到尾跑一边,每次+end[]就好,然而这个题是三层状态转移其中第二层也是类似于刚刚说的
/**************hdu28252016.3.15**************/#include <iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;#define mod 20090717int num[1<<11];int n,m,k;struct Trie{ int next[110][26],fail[110],end[110],dp[30][110][1<<10]; int root,L; int newnode() { for(int i=0;i<26;i++)next[L][i]=-1; end[L++]=0; return L-1; } void init() { L=0; root=newnode(); } void insert(char buf[],int id) { int len=strlen(buf); int now=root; for(int i=0;i<len;i++) { if(next[now][buf[i]-'a']==-1) next[now][buf[i]-'a']=newnode(); now=next[now][buf[i]-'a']; } end[now]|=(1<<id); } void build() { queue<int>Q; fail[root]=root; for(int i=0;i<26;i++) if(next[root][i]==-1) next[root][i]=root; else { fail[next[root][i]]=root; Q.push(next[root][i]); } while(!Q.empty()) { int now=Q.front(); Q.pop(); end[now]|=end[fail[now]]; for(int i=0;i<26;i++) if(next[now][i]==-1) next[now][i]=next[fail[now]][i]; else { fail[next[now][i]]=next[fail[now]][i]; Q.push(next[now][i]); } } } int solve() { memset(dp,0,sizeof(dp)); dp[0][0][0]=1;///!!! for(int i=0;i<n;i++) { for(int j=0;j<L;j++) { for(int k=0;k<(1<<m);k++) { if(dp[i][j][k]>0) for(int x=0;x<26;x++) { int newi=i+1; int newj=next[j][x];//!! int newk=k|end[newj]; dp[newi][newj][newk]+=dp[i][j][k]; dp[newi][newj][newk]%=mod; } } } } int ans=0; for(int i=0;i<(1<<m);i++) { if(num[i]<k) continue; for(int j=0;j<L;j++) { ans+=dp[n][j][i]; ans%=mod; } } return ans; }}ac;char buf[200];int main(){ //freopen("cin.txt","r",stdin); num[0]=0; for(int i=0;i<(1<<10);i++) { num[i]=0; for(int j=0;j<10;j++) if(i&(1<<j)) num[i]++; } while(~scanf("%d%d%d",&n,&m,&k)) { if(n==0&&m==0&&k==0) break; ac.init(); for(int i=0;i<m;i++) { scanf("%s",buf); ac.insert(buf,i); } ac.build(); printf("%d\n",ac.solve()); } return 0;}
0 0
- hdu2825Wireless Password【ac自动机+dp状态压缩】
- hdu 2825 Wireless Password AC自动机+状态压缩DP
- HDU 2825 Wireless Password(AC自动机+状态压缩DP)
- uva1076 - Password Suspects AC自动机+状态压缩DP
- HDU 2825 Wireless Password AC自动机 + 状态压缩DP
- HDU2825 Wireless Password AC自动机+状态压缩DP
- HDU 2825 Wireless Password (AC自动机 + 状态压缩DP)
- hdu 2825 Wireless Password (ac自动机+状态压缩dp)
- HDU 2825 Wireless Password (AC自动机+DP+状态压缩)
- 【HDU2825】Wireless Password,AC自动机+状态压缩DP
- [HDU 2825] Wireless Password (AC自动机+状态压缩DP)
- HDU 2825 Wireless Password(AC自动机+状态压缩DP)
- HDU 2825 Wireless Password(AC自动机 + 状态压缩DP 入门)
- HDU 3247 AC自动机 + 状态压缩dp
- HDU 4534(ac自动机+状态压缩dp)
- hdu 2825 ac自动机+状态压缩dp
- AC自动机+状态压缩DP+hdu2825
- hdu2825 AC自动机+状态压缩DP
- 学霸的迷宫 ----最短路径---广搜
- 遍历目录下的所有文件
- android通知栏Notification用法
- 共享内存
- MFC-贪吃蛇
- hdu2825Wireless Password【ac自动机+dp状态压缩】
- leetcode 226. Invert Binary Tree(二叉树的镜像)
- 用实力助跑2016
- 10、将数据库连接池集成到GenericityJDBCUtils中,以提高效率
- 指针+1的问题
- android studio ndk工程开发及调试指南
- PHP截取中英混合字符串(UTF-8)
- Android SDK下各目录的用途,以及在不同系统下哪些是通用的
- EasyUI DataGrid动态加载数据