hdu 2825 Wireless Password(AC自动机+压缩DP,5级)

Wireless Password

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


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
 

Source
2009 Multi-University Training Contest 1 - Host by TJU
 

Recommend
gaojie

 思路:
   先跑一遍AC自动机,建立字符串间的转移态,val[]为10个字符串的压缩态,包含哪些,然后来个DP 
    dp[n][sz][state] 前n个字符,自动机sz节点,包含状态为state
    ans 就是 n时,所有state 包含>=k的和

失误点:不管承不承认,我吧AC自动机写搓了,建FAIL时居然没更新val 导致WA的很。。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define ll(x) (1<<x)
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const int msize=200+9;
const int sig_size=26;
int ans,n,m,k;
class AC_Machine
{
  public:
    int f[msize],val[msize],last[msize],ch[msize][sig_size],sz;
  void clear()
  {
    sz=1;clr(ch[0],0);val[0]=0;
  }
  int idx(char x)
  {
    return x-'a';
  }
  void insert(char*s,int v)
  {
    int u=0,c;
    for(int i=0;s[i];++i)
    { c=idx(s[i]);
      if(!ch[u][c])///1:ch[u][c]
      {
        val[sz]=0;clr(ch[sz],0);
        ch[u][c]=sz++;
      }
      u=ch[u][c];
    }
    val[u]|=v;
  }
  void getFail()
  {
    int u,v,c,r;
    queue<int>Q;
    FOR(i,0,sig_size-1)
    { u=ch[0][i];
      if(u)
      {
        f[u]=last[u]=0;Q.push(u);
      }
    }
    while(!Q.empty())
    {
      r=Q.front();Q.pop();
      val[r]|=val[ f[r] ];//包含其的子集
      FOR(c,0,sig_size-1)
      {
        u=ch[r][c];
        if(!u){ch[r][c]=ch[ f[r] ][c];continue; }
        v=f[r];Q.push(u);
        while(v&&!ch[v][c])v=f[v];
        f[u]=ch[v][c];
        last[u]=val[ f[u] ]?f[u]:last[u];
      }
    }
  }
};
AC_Machine ac;
const int mod=20090717;
char s[19];
int dp[30][109][ll(10)];
int dig[ll(10)],num[ll(10)];
void getDig()
{
  FOR(i,0,ll(10)-1)
  {
    dig[i]=0;
    for(int j=i;j>0;j>>=1)
      if(j&1)++dig[i];
  }
}

void solve(int n,int m,int K)
{ //clr(dp,0);
  FOR(i,0,n)FOR(j,0,ac.sz-1)FOR(k,0,ll(m)-1)
  dp[i][j][k]=0;
  dp[0][0][0]=1;
  FOR(i,1,n)
  FOR(j,0,ac.sz-1)
   FOR(k,0,ll(m)-1)
   {
    if(dp[i-1][j][k])//k包含val
    {
      FOR(l,0,sig_size-1)
      {
        int p=ac.ch[j][l];
        int nk=(k|ac.val[p]);
        dp[i][p][nk]=(dp[i][p][nk]+dp[i-1][j][k])%mod;
      }
    }
   }
  int ans=0;
  FOR(i,0,ac.sz-1)FOR(j,0,ll(m)-1)
  if(dig[j]>=K)
  {
    ans=(ans+dp[n][i][j])%mod;
  }
  ans%=mod;
  printf("%d\n",ans);
}
int main()
{ int cas;
  getDig();
  while(~scanf("%d%d%d",&n,&m,&k))
  { if(n==0&&m==0&&k==0)break;
    ac.clear();
    FOR(i,1,m)
    { scanf("%s",s);
      ac.insert(s,ll(i-1));
    }
    ac.getFail();
    solve(n,m,k);
  }
}


你可能感兴趣的:(hdu 2825 Wireless Password(AC自动机+压缩DP,5级))