AC自动机(DP)

蓝桥杯算法训练Password Suspects

问题描述
  你是一个秘密犯罪组织the Sneaky Underground Smug Perpetrators of Evil Crimes and Thefts(SUSPECT)里的电脑高手。SUSPECT最新的邪恶犯罪目标是他们最大的对手the Indescribably Clever Policemen’s Club(ICPC),一切都已经准备就绪,除了一件小事:ICPC的主机密码。
  密码仅有小写字母’a’-‘z’构成。此外,通过各种偷窥,你已经确定了密码的长度,和一些(可能重叠)密码中的子串,尽管你不清楚他们出现在密码的哪个位置。
  例如,你知道密码的长度是10个字符且你观察到了子串“hello”和“world”。那么密码一定是“helloworld”或者“worldhello”。
  问题在于这些信息是否能将密码的可能数缩减到一个合理的范围内。要回答这个问题,你的任务是写一个程序判断可能的密码的总数目,如果可能的密码数目不超过42,打印出所有可能密码。
输入格式
  第一行包含两个整数N和M,分别表示密码的长度和已知的密码中子串的数量。接下来M行,每行一个密码中的已知子串。
输出格式
  第一行输出Y,Y表示可能的密码的数目。如果Y不超过42,接下来按照字典序,每行一个密码,依次输出所有可能的密码。
样例输入
10 2
hello
world
样例输出
2
helloworld
worldhello
样例输入
10 0
样例输出
141167095653376
样例输入
4 1
icpc
样例输出
1
icpc
数据规模和约定
  1<=N<=25,0<=M<=10,子串长度<=10,所有字符均为小写字母’a’-‘z’,输入数据保证答案不超过10^15

问题分析:
首先要了解什么是AC自动机,这里引用CSDN 里的博客

出自CSDN bestsort https://blog.csdn.net/bestsort/article/details/82947639

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 200;
const int maxm = 26;
LL dp[maxn][40][1<<11];
bool vis[maxn][40][1<<11];
char tmp[40];
char s[40];
int n,m;
struct Acautomata{
    int ch[maxn][maxm],val[maxn],fail[maxn],root,sz;
    int newnode(){
        val[sz] = 0;
        memset(ch[sz],0,sizeof(ch[sz]));
        return sz++;
    }
    void init(){
        sz = 0;
        root = newnode();
    }
    void insert(char *s, int v){
        int u = root, len = strlen(s);
        for(int i = 0; i < len; i++){
            int now = s[i] - 'a';
            if(!ch[u][now]){
                ch[u][now] = newnode();
            }
            u = ch[u][now];
        }
        val[u] |= v;
    }// 
    void build(){
        queue <int> q;
        fail[0] = 0;
        for(int i = 0; i < maxm; i++){
            int u = ch[0][i];
            if(u){
                q.push(u); fail[u] = 0;
            }
        }
        while(q.size()){
            int u = q.front(); q.pop();
            for(int i = 0; i < maxm; i++){
                int v = ch[u][i];
                if(!v){
                    ch[u][i] = ch[fail[u]][i];
                    continue;
                }
                q.push(v);
                int j = fail[u];
                while(j && !ch[j][i]) j = fail[j];
                fail[v] = ch[j][i];
                val[v] |= val[fail[v]];
            }
        }
    }// 
    LL dfs(int u,int len,int st)
    {
        if(vis[u][len][st]) return dp[u][len][st];
        vis[u][len][st] = 1;
        LL &ans = dp[u][len][st];
        if(len == n){
            if(st == (1<<m)-1) return ans = 1;
            else{
                return ans = 0;
            }
        }
        ans = 0;
        for(int i = 0; i < maxm; i++){
            ans += dfs(ch[u][i], len+1, st|val[ch[u][i]]);
        }
        return ans;
    }
    void print_path(int u,int len,int st)
    {
        if(len == n)
        {
            for(int i = 1; i <= len; i++) printf("%c",tmp[i]);
            printf("\n");
            return ;
        }
        for(int i = 0; i < maxm; i++)
        {
            tmp[len+1] = 'a' + i;
            if(dp[ch[u][i]][len+1][st|val[ch[u][i]]])
            {
                print_path(ch[u][i],len+1,st|val[ch[u][i]]);
            }
        }
    }
}ac;//自定义了一个 
int main()
{
    int ks = 1;
    while(scanf("%d%d",&n,&m) != EOF)
    {
        if(n == 0 && m == 0) break;
        ac.init();// 
        for(int i = 0; i < m; i++){
            scanf("%s",s);
            ac.insert(s, 1<<i);
        }
        ac.build();// 
        memset(vis,0,sizeof(vis));
        //memset(dp,0,sizeof(dp));
        LL ans = ac.dfs(0,0,0);
        //printf("%lld\n",ans);
        printf("%lld\n",ans);
        if(ans <= 42)
            ac.print_path(0,0,0);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值