1. 程式人生 > >HDU 2825 Wireless Password(AC自動機)

HDU 2825 Wireless Password(AC自動機)

accept more scanf mission content ring ont for each notes

Wireless Password

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


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 分析:這道題涉及到了多個字符串的匹配,我們可以想到AC自動機,一開始看這道題的狀態數很多,構造單詞的方式有很多種,所以我們需要進行狀態壓縮 dp[i][j][k]表示當前的字符串長度為i,在自動機的結點j上,並且出現過的表列裏的字符串為k,k包含了裏面出現的字符串的信息 這裏的num數組用來存特征值k對應的單詞數量 代碼如下:
#include <stdio.h>
#include 
<algorithm> #include <iostream> #include <string.h> #include <queue> using namespace std; int num[1<<10+10]; int dp[30][110][1<<10]; const int MOD=20090717; int n,m,s; struct Trie { int Next[110][26];//26是這裏討論26個小寫字母的情況,根據情況修改 int fail[110],end[110];//end數組表示以該節點結尾的字符串的數量 int root,L;//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];//該段的最後一個節點匹配後,跳到擁有最大公共後綴的fail節點繼續匹配 else { fail[Next[now][i]]=Next[fail[now]][i];//當前節點的fail節點等於它前驅節點的fail節點的後繼節點 Q.push(Next[now][i]); } } } int solve() { int ans=0; for(int i=0;i<=n;i++) for(int j=0;j<L;j++) for(int k=0;k<(1<<m);k++) dp[i][j][k]=0; 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 r=0;r<26;r++) { int newi=i+1; int newj=Next[j][r]; int newk=(k|end[newj]); dp[newi][newj][newk]+=dp[i][j][k]; dp[newi][newj][newk]%=MOD; // cout<<newi<<" "<<newj<<" "<<newk<<" "<<dp[newi][newj][newk]<<endl; } } } for(int k=0;k<(1<<m);k++) { if(num[k]<s)continue; for(int j=0;j<L;j++) { ans+=dp[n][j][k]; ans%=MOD; } } return ans; } }; char buf[110]; Trie ac; int main() { 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,&s)!=EOF){ if(n==0&&m==0&&s==0)break; ac.init(); for(int i=0;i<m;i++) { scanf("%s",buf); ac.insert(buf,i); } ac.build(); cout<<ac.solve()<<endl; } return 0; }

HDU 2825 Wireless Password(AC自動機)