UVALive 4126 Password Suspects(AC自动机 套 DP)

【题意】现在有个长度已知的字符串,你知道一些它的子串,问你这个字符串的可能的种数(这些子串可以重叠),如果种数 <= 42,那么就把按照字典序输出来。

【解题方法】先构造AC自动机,设 d[ u ][ len ][ st ]表示最后一个是 u ,已选长度为 len ,状态为 st 的剩余种数,则有方程:dp[ u ][ len ][ st ] = SIGMA(dp[ ch[u][i] ][ len+1 ][ st|val[ch[u][i]] ], 0 <= i < SIGMA_SIZE)。这里有一点要注意,由于一个节点有可能是很多字符串的终点,AC自动机 insert之后,在 get_fail 那里也要更新 val。输出结果那里,也是递归打印,不过有很重要的一个地方,那就是只有 d(u,len,st)> 0,才递归下去!还有这里AC自动机要是改造版的那种,正好做 DP。

【代码君】

//
//Created by just_sort 2016/10/12
//Copyright (c) 2016 just_sort.All Rights Reserved
//

#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("Case %d: %lld suspects\n",ks++,ans);
        if(ans <= 42)
            ac.print_path(0,0,0);
    }
    return 0;
}


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值