UVA 1076 - Password Suspects(AC自动机+DP)

UVA 1076 - Password Suspects

题目链接

题意:一个密码,给定m个已知子串,求这个密码最多有几种表示方式,如果小于42种,就输出这些密码

思路:先利用已有子串构造AC自动机,需要改造一下的地方是每个叶子结点为(1<<i),然后构造next数组,在状态图上进行dp,dp[i][j][k]表示在结点i,长度j,已有子串集合为k的种数,进行状态转移即可,最后判断一下答案是否不大于42,如果是再根据之前求出的dp状态去进行输出即可

代码:

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;

typedef long long ll;
const int MAXNODE = 105;
const int SIGMA_SIZE = 26;
const int N = 30;
const int M = (1<<10) + 5;

int n, m;

struct AutoMac {
    
    int ch[MAXNODE][SIGMA_SIZE];
    int val[MAXNODE];
    int next[MAXNODE];
    int last[MAXNODE];
    ll dp[MAXNODE][N][M];
    int out[N];

    int sz;
    
    void init() {
	sz = 1; 
	memset(ch[0], 0, sizeof(ch[0]));
    }

    int idx(char c) {
	return c - 'a';
    }

    void insert(char *str, int v) {
	int n = strlen(str);
	int u = 0;
	for (int i = 0; i < n; i++) {
	    int c = idx(str[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] |= (1<<v);
    }

    void getnext() {
	queue<int> Q;
	next[0] = 0;	
	for (int c = 0; c < SIGMA_SIZE; c++) {
	    int u = ch[0][c];
	    if (u) {next[u] = 0; Q.push(u); last[u] = 0;}
	}

	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[next[r]][c];
		    continue;
		}
		Q.push(u);
		int v = next[r];
		while (v && !ch[v][c]) v = next[v];
		next[u] = ch[v][c];
		val[u] |= val[next[u]];
		//last[u] = val[next[u]] ? next[u] : last[next[u]];
	    }
	}
    }

    /*
    void print(int i, int j) {
	if (j) {
	    //printf("%d: %d\n", i, val[j]);
	    print(i, last[j]);
	}
    }

    void find(char *T) {
	int n = strlen(T);
	int j = 0;
	for (int i = 0; i < n; i++) {
	    int c = idx(T[i]);
	    j = ch[j][c];
	    if (val[j]) print(i, j);
	    else if (last[j]) print(i, last[j]);
	}
    }*/
    
    ll dfs(int now, int len, int st) {
	ll &ans = dp[now][len][st];
	if (ans != -1) return ans;
	ans = 0;
	if (len == n) {
	    if (st == (1<<m) - 1) return ans = 1;
	    return ans = 0;
	}
	for (int i = 0; i < SIGMA_SIZE; i++)
	    ans += dfs(ch[now][i], len + 1, st|val[ch[now][i]]);
	return ans;
    }

    void print(int now, int len, int st) {
	if (len == n) {
	    for (int i = 0; i < len ;i++)
		printf("%c", out[i] + 'a');
	    printf("\n");
	    return;
	}
	for (int i = 0; i < SIGMA_SIZE; i++) {
	    if (dp[ch[now][i]][len + 1][st|val[ch[now][i]]] > 0) {
		out[len] = i;
		print(ch[now][i], len + 1, st|val[ch[now][i]]);
	    }
	}
    }

    void solve() {
	char str[15];
	for (int i = 0; i < m; i++) {
	    scanf("%s", str);
	    insert(str, i);
	}
	getnext();
	memset(dp, -1, sizeof(dp));
	ll ans = dfs(0, 0, 0);
	printf("%lld suspects\n", ans);
	if (ans <= 42)
	    print(0, 0, 0);
    }

} gao;


int main() {
    int cas = 0;
    while (~scanf("%d%d", &n, &m) && n || m) {
	printf("Case %d: ", ++cas);
	gao.init();
	gao.solve();
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值