Lost's revenge HDU - 3341 ad自动机+进制压缩

52 篇文章 0 订阅
13 篇文章 0 订阅

一、内容

 Lost and AekdyCoin are friends. They always play "number game"(A boring game based on number theory) together. We all know that AekdyCoin is the man called "nuclear weapon of FZU,descendant of Jingrun", because of his talent in the field of number theory. So Lost had never won the game. He was so ashamed and angry, but he didn't know how to improve his level of number theory.

One noon, when Lost was lying on the bed, the Spring Brother poster on the wall(Lost is a believer of Spring Brother) said hello to him! Spring Brother said, "I'm Spring Brother, and I saw AekdyCoin shames you again and again. I can't bear my believers were being bullied. Now, I give you a chance to rearrange your gene sequences to defeat AekdyCoin!".

It's soooo crazy and unbelievable to rearrange the gene sequences, but Lost has no choice. He knows some genes called "number theory gene" will affect one "level of number theory". And two of the same kind of gene in different position in the gene sequences will affect two "level of number theory", even though they overlap each other. There is nothing but revenge in his mind. So he needs you help to calculate the most "level of number theory" after rearrangement. 

Input

There are less than 30 testcases.
For each testcase, first line is number of "number theory gene" N(1<=N<=50). N=0 denotes the end of the input file.
Next N lines means the "number theory gene", and the length of every "number theory gene" is no more than 10.
The last line is Lost's gene sequences, its length is also less or equal 40.
All genes and gene sequences are only contains capital letter ACGT.

Output

For each testcase, output the case number(start with 1) and the most "level of number theory" with format like the sample output.

Sample Input

3
AC
CG
GT
CGAT
1
AA
AAA
0

Sample Output

Case 1: 3
Case 2: 2

二、思路

  • 直接求构造的串包含模式串的次数。 那么可以构造dp【j】【A】【T】【G】【C】代表在ac自动机上位于j节点,用了A个‘A’, T个‘T’,G个‘G’,C个‘C’的最大level。
  • 由于ATGC最多40, 那么404040*40的话空间肯定无法满足。 由于ATGC加起来最多40个,那么我们可以进行压缩,压缩后状态数11 * 11 * 11 * 11。
    在这里插入图片描述

三、代码

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N = 55, M = 505, INF = 0x3f3f3f3f;
int n, T, len, tr[M][4], ne[M], fail[M], num[5], b[5], f[M][11 * 11 * 11 * 11 + 10];
char s[N];
int getId(char c) {
	if (c == 'A') return 0;
	if (c == 'T') return 1;
	if (c == 'G') return 2; return 3;
} 
void add() {
	int p = 0;
	for (int i = 0; s[i]; i++) {
		int j = getId(s[i]); 
		if (!tr[p][j]) tr[p][j] = ++len;
		p =	tr[p][j];
	}
	fail[p]++;
}
void build() {
	queue<int> q;
	for (int j = 0; j < 4; j++) {
		if (tr[0][j]) q.push(tr[0][j]);
	}
	while (!q.empty()) {
		int p = q.front(); q.pop();
		for (int j = 0; j < 4; j++) {
			int c = tr[p][j];
			if (!c) tr[p][j] = tr[ne[p]][j];
			else {
				ne[c] = tr[ne[p]][j];
				fail[c] += fail[ne[c]]; //加上子串出现的次数 
				q.push(c); 
			}
		}
	}
}
void init() {
	memset(num, 0, sizeof(num));
	memset(b, 0, sizeof(b));
	memset(tr, 0, sizeof(tr));
	memset(ne, 0, sizeof(ne)); len = 0; 
	memset(fail, 0, sizeof(fail));
	memset(f, -0x3f, sizeof f);
	for (int i = 1; i <= n; i++) {
		scanf("%s", s); add();
	}
	build();
}
void solve() {
	scanf("%s", s);
	for (int i = 0; s[i]; i++) {
		int id = getId(s[i]); num[id]++;
	}
	b[0] = 1; b[1] = num[0] + 1;
	b[2] = b[1] * (num[1] + 1);
	b[3] = b[2] * (num[2] + 1);
	f[0][0] = 0;
	n = strlen(s);
	for (int A = 0; A <= num[0]; A++) {
		for (int T = 0; T <= num[1]; T++) {
			for (int G = 0; G <= num[2]; G++) {
				for (int C = 0; C <= num[3]; C++) {
					for (int j = 0; j <= len; j++) {
					 	int st = A + T*b[1] + G*b[2] + C*b[3];
						if (f[j][st] == -INF) continue; 
						//上一状态(j, st)--> (tj, nst);
						//枚举下一状态的字符选谁
						for (int k = 0; k < 4; k++) {
							if (k == 0 && A >= num[k]) continue;//已经选完了这个字符 
							if (k == 1 && T >= num[k]) continue;//已经选完了这个字符 
							if (k == 2 && G >= num[k]) continue;//已经选完了这个字符 
							if (k == 3 && C >= num[k]) continue;//已经选完了这个字符
							int nst = st + b[k];
							int tj = tr[j][k];
							f[tj][nst] = max(f[tj][nst], f[j][st] + fail[tj]); 
						} 
 					}
				}
			}
		}
	}
	int ans = 0, st = num[0] + b[1] * num[1] + b[2] * num[2] + b[3] * num[3];
	for (int j = 0; j <= len; j++) ans = max(ans, f[j][st]);
	printf("Case %d: %d\n", ++T, ans);
} 
int main() {
	while (scanf("%d", &n), n) {
		init();
		solve();
	}
	return 0;
} 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值