1. 程式人生 > >Wireless Password(AC自動機+dp)

Wireless Password(AC自動機+dp)

Wireless Password

Time Limit:1000 MS
Memory Limit: 32768 K

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

題目大意:

字符集為’a~z’,
給定 m 個串,詢問長度為 n 並且包含至少 k 個給定串的字串個數。
0 k m 10 , n 25










解:

很簡單的一道題,不過是第一次寫自動機上dp。
f(i,j,k)表示串長為i,自動機上走到j,並且匹配了k的字串方案數。k是一個狀態壓縮。轉移我們只需要列舉下一個字元找到該往哪裡轉移即可。
可以優化:1,每個節點預處理每個字元會轉移到哪個節點。2,spfa處理每個串結尾到另一串結尾的轉移。
但是這道題都不需要。

code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define mod 20090717
using namespace std;
struct lxy{
    int to[26],fail,end;
}a[105];
int n,k,m,cnt,root,tep,ans;
int f[26][105][1024];
char s[15];
queue <int> d;

void insert(int &u,int p){
    if(u==0) u=++cnt;
    if(s[p]==0){
        a[u].end|=tep;
        return;
    }
    insert(a[u].to[s[p]-'a'],p+1);
}

void bfs(){
    a[root].fail=root;
    for(int i=0;i<=25;i++)
      if(a[root].to[i]!=0){
          a[a[root].to[i]].fail=root;
          d.push(a[root].to[i]);
      }
    while(!d.empty()){
        int now=d.front();d.pop();
        for(int i=0;i<=25;i++)
          if(a[now].to[i]!=0){
              d.push(a[now].to[i]);
              int p;for(p=a[now].fail;p!=root&&a[p].to[i]==0;p=a[p].fail);
              if(a[p].to[i]==0) a[a[now].to[i]].fail=root;
              else a[a[now].to[i]].fail=a[p].to[i],a[a[now].to[i]].end|=a[a[p].to[i]].end;
          }
    }
}

int cot(int u,int t){
    for(;a[u].to[t]==0&&u!=root;u=a[u].fail);
    if(a[u].to[t]==0) return root;
    else return a[u].to[t];
}

int main()
{
    while(true){
        scanf("%d%d%d",&n,&m,&k);
        if(n==0&&m==0&&k==0) return 0;
        tep=0;root=1;cnt=1;ans=0;
        memset(a,0,sizeof(a));
        memset(f,0,sizeof(f));
        for(int i=1;i<=m;i++)
          scanf("%s",s+1),tep=(1<<(i-1)),insert(root,1);
        bfs();
        f[0][1][0]=1;
        for(int i=0;i<n;i++)
          for(int j=1;j<=cnt;j++)
            for(int q=0;q<=(1<<m)-1;q++)
              if(f[i][j][q]!=0)
                for(int t=0;t<=25;t++){
                      int fx=cot(j,t);
                      (f[i+1][fx][q|a[fx].end]+=f[i][j][q])%=mod;
                  }
        for(int j=1;j<=cnt;j++)
          for(int q=0;q<=(1<<m)-1;q++){
              int y=0;
              for(int i=0;i<m;i++)
                if((q&(1<<i))==(1<<i))
                  y++;
              if(y>=k) (ans+=f[n][j][q])%=mod;
          }
        printf("%d\n",ans);
    }
}