Lost's revenge HDU - 3341 ad自動機+進制壓縮

一、內容

 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;
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章