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.
 

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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 新买的鼠标没反应怎么办 联想笔记本触屏鼠标失灵怎么办 无线鼠标接收器丢了怎么办 联想笔记本系统重装失败怎么办 联想笔记本屏幕闪屏怎么办 种植牙螺钉掉了怎么办 水管牙断里面了怎么办 水龙头起泡器不起泡怎么办 14mm乘8mm残留怎么办 宝宝吃了螺丝冒怎么办 收割机滚筒皮带轮键槽滚了怎么办 微信界面变小了怎么办 拉杆箱螺丝掉了怎么办 洗衣机应急门锁没有拉绳怎么办? 奔驰glc发动机声音大怎么办 淋膜机模具螺丝拧不动怎么办 一字螺丝滑丝了怎么办 螺丝拧歪卡住了怎么办 车牌螺丝拧歪了怎么办 空心墙打膨胀螺丝打不上怎么办 沉孔内六角螺丝滑丝怎么办 内六角螺丝滑了怎么办? 三色灯不变光了怎么办 卧室灯不变色了怎么办 圆柱齿轮减速机噪音大怎么办 轴与套间隙生锈怎么办 汽车停小区被刮怎么办 下楼梯摔跤了 屁股疼 怎么办 剧烈咳嗽震的肚子疼怎么办 饺子粘在盘子上怎么办 生饺子粘在盘子怎么办 饺子粘在案板上怎么办 饺子冷冻粘起了怎么办 冰箱饺子冻住了怎么办 水饺都冻一块了怎么办 wps卸载了后文件打不开怎么办 六角螺母拧滑了怎么办 梅花内六角螺丝扭滑丝了怎么办 眼镜螺丝滑丝了怎么办 大螺丝拆不下来怎么办 一字螺丝扭不动怎么办