Description

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
 

题意:给你n个子串和一个母串,让你重排母串最多能得到多少个子串。

思路:如果ACGT的个数依次是num[0],num[1],num[2],num[3],将这四个数压缩成一个数,降低内存,然后动归dp[i][status]表示到自己主动机上i状态此时ACGT个数的状态时的最少

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int inf = 0x3f3f3f3f; struct Trie {
int nxt[510][4], fail[510], end[510];
int root, cnt; int newNode() {
for (int i = 0; i < 4; i++)
nxt[cnt][i] = -1;
end[cnt++] = 0;
return cnt - 1;
}
void init() {
cnt = 0;
root = newNode();
}
int getch(char ch) {
if (ch == 'A') return 0;
else if (ch == 'C') return 1;
else if (ch == 'G') return 2;
return 3;
}
void insert(char str[]) {
int len = strlen(str);
int now = root;
for (int i = 0; i < len; i++) {
if (nxt[now][getch(str[i])] == -1)
nxt[now][getch(str[i])] = newNode();
now = nxt[now][getch(str[i])];
}
end[now]++;
}
void build() {
queue<int> q;
fail[root] = root;
for (int i = 0; i < 4; i++) {
if (nxt[root][i] == -1)
nxt[root][i] = root;
else {
fail[nxt[root][i]] = root;
q.push(nxt[root][i]);
}
} while (!q.empty()) {
int now = q.front();
q.pop();
end[now] += end[fail[now]]; //好像每次都是这里
for (int i = 0; i < 4; i++) {
if (nxt[now][i] == -1)
nxt[now][i] = nxt[fail[now]][i];
else {
fail[nxt[now][i]] = nxt[fail[now]][i];
q.push(nxt[now][i]);
}
}
}
}
int dp[510][11*11*11*11+5];
int bit[4], num[4];
int solve(char str[]) {
int len = strlen(str);
memset(num, 0, sizeof(num));
for (int i = 0; i < len; i++)
num[getch(str[i])]++;
bit[0] = (num[1]+1) * (num[2]+1) * (num[3]+1);
bit[1] = (num[2]+1) * (num[3]+1);
bit[2] = (num[3]+1);
bit[3] = 1;
memset(dp, -1, sizeof(dp));
dp[root][0] = 0;
for (int A = 0; A <= num[0]; A++)
for (int B = 0; B <= num[1]; B++)
for (int C = 0; C <= num[2]; C++)
for (int D = 0; D <= num[3]; D++) {
int s = A * bit[0] + B * bit[1] + C * bit[2] + D * bit[3];
for (int i = 0; i < cnt; i++)
if (dp[i][s] >= 0) {
for (int k = 0; k < 4; k++) {
if (k == 0 && A == num[0]) continue;
if (k == 1 && B == num[1]) continue;
if (k == 2 && C == num[2]) continue;
if (k == 3 && D == num[3]) continue;
dp[nxt[i][k]][s+bit[k]] = max(dp[nxt[i][k]][s+bit[k]], dp[i][s]+end[nxt[i][k]]);
}
}
} int ans = 0;
int status = num[0] * bit[0] + num[1] * bit[1] + num[2] * bit[2] + num[3] * bit[3];
for (int i = 0; i < cnt; i++)
ans = max(ans, dp[i][status]);
return ans;
}
} ac;
char str[50]; int main() {
int n, cas = 1;
while (scanf("%d", &n) != EOF && n) {
ac.init();
for (int i = 0; i < n; i++) {
scanf("%s", str);
ac.insert(str);
} ac.build();
scanf("%s", str);
printf("Case %d: %d\n", cas++, ac.solve(str));
}
return 0;
}

HDU - 3341 Lost&#39;s revenge(AC自己主动机+DP)的更多相关文章

  1. Hdu 3341 Lost&#39;s revenge (ac+自己主动机dp+hash)

    标题效果: 举个很多种DNA弦,每个字符串值值至1.最后,一个长字符串.要安排你最后一次另一个字符串,使其没事子值和最大. IDEAS: 首先easy我们的想法是想搜索的!管她3721..直接一个字符 ...

  2. HDU - 2825 Wireless Password(AC自己主动机+DP)

    Description Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless ne ...

  3. HDU - 4758 Walk Through Squares (AC自己主动机+DP)

    Description   On the beaming day of 60th anniversary of NJUST, as a military college which was Secon ...

  4. POJ 2778 DNA Sequence (AC自己主动机 + dp)

    DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...

  5. hdu4758 Walk Through Squares (AC自己主动机+DP)

    Walk Through Squares Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others ...

  6. poj 3691 DNA repair(AC自己主动机+dp)

    DNA repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5877   Accepted: 2760 Descri ...

  7. hdu4057 Rescue the Rabbit(AC自己主动机+DP)

    Rescue the Rabbit Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  8. HDU - 4511 小明系列故事――女友的考验(AC自己主动机+DP)

    Description 最终放寒假了,小明要和女朋友一起去看电影.这天,女朋友想给小明一个考验,在小明正准备出发的时候.女朋友告诉他.她在电影院等他,小明过来的路线必须满足给定的规则:  1.如果小明 ...

  9. Hdu 2457 DNA repair (ac自己主动机+dp)

    题目大意: 改动文本串的上的字符,使之不出现上面出现的串.问最少改动多少个. 思路分析: dp[i][j]表示如今 i 个字符改变成了字典树上的 j 节点. 然后顺着自己主动机一直转移方程. 注意合法 ...

随机推荐

  1. android 动画xml属性具体解释

    /** * 作者:crazyandcoder * 联系: * QQ : 275137657 * email: lijiwork@sina.com * 转载请注明出处! */ android 动画属性具 ...

  2. SQL 增删改查(具体)

    一.增:有3种方法 1.使用insert插入单行数据: insert [into] <表名> [列名] values <列值> insert into Strdents (na ...

  3. .Net 扩展的使用

    Product.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; name ...

  4. kali之EtterCap学习

    EtterCap是一个基于ARP地址欺骗方式的网络嗅探工具,主要适用于交换局域网络.借助于EtterCap嗅探软件,渗透测试人员可以检测网络内明文数据通讯的安全性,及时采取措施,避免敏感的用户名/密码 ...

  5. python 内存中写入文件(read读取不到文件解决)

    from io import StringIO a = StringIO.StringIO('title') a.write('content1\n') a.write('content2') a.s ...

  6. Java表单设计器orbeon点滴

    包含表单设计器和运行展现 一个完整的应用 页面部分都是使用XML和XHTML进行服务端的组合出来的,具体逻辑有些复杂 设计器缺少一个最常用的:repeat,如果需要只能手动编写代码(参考官方文档步骤有 ...

  7. 运行 CMD 时,參数加引號常见问题

    在调用 CMD 时.如脚本中用 WScript.Shell 调用. 假设參数中有包括空格的长路径名时,必需要加引號才干正确被识别. 是的,大家都知道要加引號.但怎么加却easy被误解.这个问题,不时地 ...

  8. apache wicket 7.X之HelloWorld

    Wicket是什么 Wicket一个开发Java Web应用程序框架. 它使得开发web应用程序变得easy而轻松. Wicket利用一个POJO data beans组件使得它能够与不论什么持久层技 ...

  9. Python笔记---错误笔记

    Python---错误笔记 1. Python编码问题: 我们在编写 Python 脚本时,往往会写上中文凝视. 可是有时候,当我们执行程序时.却发现例如以下错误:SyntaxError: Non-A ...

  10. code -结合实例总结代码下拉流程

    1.查看手机需要的版本 1)如果手机本来就可以正常工作,可以使用指令 zhangshuli@zhangshuli-MS-:~/Desktop/day_note/plan$ adb shell getr ...