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 m 个串,询问长度为 n n 并且包含至少 k k 个给定串的字符串个数。
0km10,n25 0 ≤ k ≤ m ≤ 10 , n ≤ 25










解:

很简单的一道题,不过是第一次写自动机上dp。
f(i,j,k)表示串长为i,自动机上走到j,并且匹配了k的字符串方案数。k是一个状态压缩。转移我们只需要枚举下一个字符找到该往哪里转移即可。
可以优化:1,每个节点预处理每个字符会转移到哪个节点。2,spfa处理每个串结尾到另一串结尾的转移。
但是这道题都不需要。

code:

#include
#include
#include
#include
#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;ifor(int j=1;j<=cnt;j++)
            for(int q=0;q<=(1<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<1;q++){
              int y=0;
              for(int i=0;iif((q&(1<1<if(y>=k) (ans+=f[n][j][q])%=mod;
          }
        printf("%d\n",ans);
    }
}

你可能感兴趣的:(字符串处理)