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

本文介绍了解决UVA1076-PasswordSuspects问题的方法,通过构建AC自动机并进行DP状态转移来找出所有可能的密码组合,特别关注了当解的数量不多于42种情况下的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}


### 使用Eclipse Memory Analyzer进行内存分析 #### 启用堆转储功能 为了能够在发生`OutOfMemoryError`时自动生成堆转储文件,在Eclipse运行配置中添加参数 `-XX:+HeapDumpOnOutOfMemoryError`[^4]。 #### 导入并打开堆转储文件 启动Eclipse Memory Analyzer Tool (MAT),通过菜单栏中的“File -> Open Heap Dump...”,选择之前生成的.hprof文件来加载它。一旦导入完成,工具会自动解析数据,并提供初步报告概述当前Java应用程序的状态以及可能存在的问题区域。 #### 查看概要视图 在成功打开了一个heap dump之后,默认情况下将会显示Overview页面。这里提供了关于整个JVM heap大小的信息,包括类实例的数量统计、对象占用的空间比例等重要指标。这有助于快速识别是否有异常大量的对象被创建或是某些特定类型的对象占用了过多资源。 #### 利用Dominator Tree定位大对象 导航到"Dominators"标签页可以查看支配树(Dominator Tree), 它展示了哪些根节点控制着其他子节点的存在与否;换句话说就是找出那些持有大量引用链顶端位置的对象。对于寻找潜在泄漏源非常有用,因为这些所谓的“垃圾收集器根”往往正是造成内存泄露的关键所在[^3]。 #### 执行直方图查询 Histogram选项卡列出了所有已知类及其对应的实例数目和所消耗字节数量。这对于发现重复加载相同类型的数据结构特别有帮助——如果某个ClassInstance数量远超预期,则可能是由于缓存策略不当或者是循环依赖关系引起的。 #### 自动化检测内存泄漏 借助于内置插件或脚本支持,Eclipse MAT能够实现一键式的自动化堆栈跟踪分析流程,从而大大简化了查找复杂应用内部隐藏较深的问题的过程。例如,“Leak Suspects Report”可以直接指出最有可能导致内存溢出的地方,并给出改进建议。 ```java // 示例代码片段用于说明如何触发堆转储操作 try { // Your code here... } catch(OutOfMemoryError e){ System.gc(); // 尝试清理无用对象 throw new RuntimeException("Out Of Memory",e); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值