uva1076 - Password Suspects AC自动机+状态压缩DP

You are the computer whiz for the secret organization known as the Sneaky Underground Smug Perpetrators of Evil Crimes and Thefts. The target for SUSPECT's latest evil crime is their greatest foe, theIndescribably Clever Policemen's Club, and everything is prepared. Everything, except for one small thing: the secret password for ICPC's main computer system.

The password is known to consist only of lowercase letters `a'-`z'. Furthermore, through various sneaky observations, you have been able to determine the length of the password, as well as a few (possibly overlapping) substrings of the password, though you do not know exactly where in the password they occur.

For instance, say that you know that the password is 10 characters long, and that you have observed the substrings ``hello" and ``world". Then the password must be either ``helloworld" or ``worldhello".

The question is whether this information is enough to reduce the number of possible passwords to a reasonable amount. To answer this, your task is to write a program that determines the number of possible passwords and, if there are at most 42 of them, prints them all.

Input 

The input file contains several test cases. Each test case begins with a line containing two integersN and M (1N25, 0M10), giving the length of the password and the number of known substrings respectively. This is followed byM lines, each containing a known substring. Each known substring consists of between 1 and 10 lowercase letters `a'-`z'.

The last test case is followed by a line containing two zeroes.

Output 

For each test case, print the case number (beginning with 1) followed by `Ysuspects', where Y is the number of possible passwords for this case. If the number of passwords is at most 42, then output all possible passwords in alphabetical order, one per line.

The input will be such that the number of possible passwords at most 1015.

Sample Input 

10 2 
hello 
world 
10 0 
4 1 
icpc 
0 0

Sample Output 

Case 1: 2 suspects 
helloworld 
worldhello 
Case 2: 141167095653376 suspects 
Case 3: 1 suspects 
icpc

  有多少个长度为N的串包含所有给的子串。

  建立AC自动机,第i个串的val的值异或上2^i。不能直接赋值2^i,因为如果两个串相同这样就不对了。

  dp[n][u][s]表示当前串长度为n,在节点u,已经经过的子串状态为s(经过的位为1),前缀为当前串的有多少种。当n==N时,如果s==(1<<M)-1返回1,否则返回0。

  先记忆化搜一遍,如果答案大于42,就再搜索一遍,把满足的串加入vector。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#define MAXNODE 110
#define SIGMA_SIZE 26
using namespace std;
typedef long long LL;
int N,M;
LL dp[30][MAXNODE][1050];
char str[20];
vector<string> V;
struct AC{
    int ch[MAXNODE][SIGMA_SIZE],f[MAXNODE],val[MAXNODE],sz;
    void init(){
        memset(ch[0],0,sizeof(ch[0]));
        val[0]=0;
        sz=1;
    }
    int idx(char c){
        return c-'a';
    }
    void insert(char *s,int v){
        int u=0;
        for(int i=0;s[i];i++){
            int c=idx(s[i]);
            if(!ch[u][c]){
                memset(ch[sz],0,sizeof(ch[sz]));
                val[sz]=0;
                ch[u][c]=sz++;
            }
            u=ch[u][c];
        }
        val[u]|=v;
    }
    void get_fail(){
        queue<int> q;
        f[0]=0;
        for(int c=0;c<SIGMA_SIZE;c++){
            int u=ch[0][c];
            if(u){
                f[u]=0;
                q.push(u);
            }
        }
        while(!q.empty()){
            int r=q.front();
            q.pop();
            for(int c=0;c<SIGMA_SIZE;c++){
                int u=ch[r][c];
                if(!u){
                    ch[r][c]=ch[f[r]][c];
                    continue;
                }
                q.push(u);
                f[u]=ch[f[r]][c];
                val[u]|=val[f[u]];
            }
        }
    }
}ac;
LL DP(int cur,int u,int s){
    LL &ans=dp[cur][u][s];
    if(ans!=-1) return ans;
    ans=0;
    if(cur>=N){
        if(s==(1<<M)-1) ans=1;
        return ans;
    }
    for(int c=0;c<SIGMA_SIZE;c++) ans+=DP(cur+1,ac.ch[u][c],s|ac.val[ac.ch[u][c]]);
    return ans;
}
void get_ans(int cur,int u,int s,string str){
    if(dp[cur][u][s]<=0) return;
    if(cur>=N){
        V.push_back(str);
        return;
    }
    for(int c=0;c<SIGMA_SIZE;c++) get_ans(cur+1,ac.ch[u][c],s|ac.val[ac.ch[u][c]],str+char(c+'a'));
}
int main(){
    freopen("in.txt","r",stdin);
    int cas=0;
    while(scanf("%d%d",&N,&M)!=EOF&&(N||M)){
        ac.init();
        for(int i=0;i<M;i++){
            scanf("%s",str);
            ac.insert(str,1<<i);
        }
        ac.get_fail();
        memset(dp,-1,sizeof(dp));
        LL ans=DP(0,0,0);
        cout<<"Case "<<++cas<<": "<<ans<<" suspects"<<endl;
        if(ans<=42){
            V.clear();
            get_ans(0,0,0,"");
            sort(V.begin(),V.end());
            int L=V.size();
            for(int i=0;i<L;i++) cout<<V[i]<<endl;
        }
    }
    return 0;
}


你可能感兴趣的:(uva1076 - Password Suspects AC自动机+状态压缩DP)