博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU2825 Wireless Password 【AC自动机】【状压DP】
阅读量:4317 次
发布时间:2019-06-06

本文共 3409 字,大约阅读时间需要 11 分钟。

HDU2825 Wireless Password


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


我觉得挺好的一道题。。一开始只知道是DP,然后写了一个循环,后面发现不能重复计算,然后改成了状压DP,改成了队列实现DP,上线是步数小于等于长度,然后每一次DP转移将步数加1就好了,然后或一下当前存在的状态。

处理fail指针的时候做两个小优化,一个是在处理的时候就把fail链上的id全部统计起来,二个是把失配后跳到的节点直接储存成儿子节点
然后注意开一下取模的操作,防止TLE


#include
using namespace std;#define N 110#define Mod 20090717struct Node{ int fail,id,ch[26]; void clean(){ fail=id=0; memset(ch,0,sizeof(ch)); }}t[N];struct Node1{
int id,tmp,now;};int n,m,p,tot=0;int dp[N][26][1025],c[1025];bool vis[N][26][1025];char s[20][N];void init(){ tot=1; t[0].clean();t[1].clean(); for(int i=0;i<26;i++)t[0].ch[i]=1; memset(dp,0,sizeof(dp));}int index(char c){
return c-'a';}void insert(char *s,int id){ int len=strlen(s),u=1; for(int i=0;i
q;q.push(1); while(!q.empty()){ int u=q.front();q.pop(); for(int i=0;i<26;i++){ int w=t[u].ch[i],v=t[u].fail; while(!t[v].ch[i])v=t[v].fail; v=t[v].ch[i]; if(w){ t[w].fail=v; t[w].id|=t[v].id;//*** q.push(w); }else t[u].ch[i]=v;//*** } }}void solve(){ queue
qn; qn.push((Node1){
1,0,0}); dp[1][0][0]=1; while(!qn.empty()){ Node1 u=qn.front();qn.pop(); vis[u.id][u.tmp][u.now]=0; if(u.tmp>n)continue; for(int i=0;i<26;i++){ int id=t[u.id].ch[i],tmp=u.tmp+1,now=u.now|t[id].id; dp[id][tmp][now]+=dp[u.id][u.tmp][u.now]; if(dp[id][tmp][now]>Mod)dp[id][tmp][now]-=Mod; if(!vis[id][tmp][now]){ vis[id][tmp][now]=1; qn.push((Node1){id,tmp,now}); } } }}int count(int t){ int ans=0; while(t){ if(t&1)ans++; t>>=1; } return ans;}int main(){ for(int i=0;i<1024;i++)c[i]=count(i); while(1){ scanf("%d%d%d",&n,&m,&p); if(!n)break; init();//初始化 for(int i=1;i<=m;i++){ scanf("%s",s[i]); insert(s[i],i); } buildFail(); solve(); int ans=0,up=1<
=p) for(int j=1;j<=tot;j++){ ans+=dp[j][n][i]; if(ans>Mod)ans-=Mod; } printf("%d\n",ans); } return 0;}

转载于:https://www.cnblogs.com/dream-maker-yk/p/9676377.html

你可能感兴趣的文章
二级图片导航菜单
查看>>
<Using parquet with impala>
查看>>
07-Java 中的IO操作
查看>>
uclibc,eglibc,glibc之间的区别和联系【转】
查看>>
Java魔法堂:找外援的利器——Runtime.exec详解
查看>>
mysql数据库存放路径
查看>>
TestNG(五)常用元素的操作
查看>>
解决 Visual Studio 点击添加引用无反应的问题
查看>>
通过镜像下载Android系统源码
查看>>
python字符串格式化 %操作符 {}操作符---总结
查看>>
windows 不能在 本地计算机 启动 Apache
查看>>
iOS开发报duplicate symbols for architecture x86_64错误的问题
查看>>
Chap-6 6.4.2 堆和栈
查看>>
【Java学习笔记之九】java二维数组及其多维数组的内存应用拓展延伸
查看>>
C# MySql 连接
查看>>
sk_buff Structure
查看>>
oracle的级联更新、删除
查看>>
多浏览器开发需要注意的问题之一
查看>>
Maven配置
查看>>
HttpServletRequest /HttpServletResponse
查看>>