Lost's revenge

Time Limit: 5000ms
Memory Limit: 65535KB

This problem will be judged on HDU. Original ID: 3341
64-bit integer IO format: %I64d      Java class name: Main

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

Source

 
解题:Trie 图 + 变进制hash + 状压dp
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct Trie {
int ch[maxn][],fail[maxn],cnt[maxn],tot;
int id[];
void init() {
tot = ;
newnode();
for(int i = ; i < ; ++i) id["ACGT"[i]] = i;
}
int newnode() {
memset(ch[tot],,sizeof ch[tot]);
fail[tot] = cnt[tot] = ;
return tot++;
}
void insert(char *str,int rt = ) {
for(int i = ; str[i]; ++i) {
int &x = ch[rt][id[str[i]]];
if(!x) x = newnode();
rt = x;
}
cnt[rt]++;
}
void build(int rt = ) {
queue<int>q;
for(int i = ; i < ; ++i)
if(ch[rt][i]) q.push(ch[rt][i]);
while(!q.empty()) {
rt = q.front();
q.pop();
cnt[rt] += cnt[fail[rt]];
for(int i = ; i < ; ++i) {
int &x = ch[rt][i],y = ch[fail[rt]][i];
if(x) {
fail[x] = y;
q.push(x);
} else x = y;
}
}
}
int dp[maxn][*** + ];
int solve(char *str) {
int bt[] = {,,,},num[] = {};
for(int i = ; str[i]; ++i) ++num[id[str[i]]];
for(int i = ; i >= ; --i)
bt[i] = bt[i + ]*(num[i] + );
memset(dp,-,sizeof dp);
int ans = dp[][] = ;
for(int a = ; a <= num[]; ++a)
for(int b = ; b <= num[]; ++b)
for(int c = ; c <= num[]; ++c)
for(int d = ; d <= num[]; ++d) {
int hs = a*bt[] + b*bt[] + c*bt[] + d;
for(int i = ; i < tot; ++i) {
if(dp[i][hs] == -) continue;
for(int k = ; k < ; ++k) {
if(a == num[] && k == ) continue;
if(b == num[] && k == ) continue;
if(c == num[] && k == ) continue;
if(d == num[] && k == ) continue;
int x = dp[ch[i][k]][hs + bt[k]];
int y = dp[i][hs] + cnt[ch[i][k]];
dp[ch[i][k]][hs + bt[k]] = max(x,y);
}
}
}
int st = num[]*bt[] + num[]*bt[] + num[]*bt[] + num[]*bt[];
for(int i = ; i < tot; ++i)
ans = max(ans,dp[i][st]);
return ans;
}
} ac;
char str[maxn];
int main() {
int n,cs = ;
while(scanf("%d",&n),n){
ac.init();
while(n--){
scanf("%s",str);
ac.insert(str);
}
scanf("%s",str);
ac.build();
printf("Case %d: %d\n",cs++,ac.solve(str));
}
return ;
}

HDU 3341 Lost's revenge的更多相关文章

  1. HDU 3341 Lost's revenge AC自动机+dp

    Lost's revenge Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)T ...

  2. HDU 3341 Lost's revenge(AC自动机+DP)

    Lost's revenge Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)T ...

  3. HDU 3341 Lost's revenge ( Trie图 && 状压DP && 数量限制类型 )

    题意 : 给出 n 个模式串,最后给出一个主串,问你主串打乱重组的情况下,最多能够包含多少个模式串. 分析 : 如果你做过类似 Trie图 || AC自动机 + DP 类似的题目的话,那么这道题相对之 ...

  4. HDU 3341 Lost's revenge (AC自动机 + DP + 变进制/hash)题解

    题意:给你些分数串,给你一个主串,主串每出现一个分数串加一分,要你重新排列主串,最多几分 思路:显然这里开$40^4$去状压内存不够.但是我们自己想想会发现根本不用开那么大,因为很多状态是废状压,不是 ...

  5. HDU - 3341 Lost&#39;s revenge(AC自己主动机+DP)

    Description Lost and AekdyCoin are friends. They always play "number game"(A boring game b ...

  6. Lost's revenge - HDU 3341 (自动机+DP)

    题目大意:先给你一些子串,然后给你一个母串,母串里面的字母可以任意调换位置,问最多这个母串经过一些位置变动最多能包含多少个子串.   分析:可以比较明显的看出来的DP,先求出来ATGC分别有多少,然后 ...

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

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

  8. Lost's revenge HDU - 3341 AC自动机+DP(需要学会如何优雅的压缩状态)

    题意: 给你n个子串和一个母串,让你重排母串最多能得到多少个子串出现在重排后的母串中. 首先第一步肯定是获取母串中每个字母出现的次数,只有A T C G四种. 这个很容易想到一个dp状态dp[i][A ...

  9. HDU P3341 Lost's revenge 题解+数据生成器

    Lost and AekdyCoin are friends. They always play "number game"(A boring game based on numb ...

随机推荐

  1. 《Openstack的搭建》RHEL6.5

    Openstack就是搭建一个较为完整的虚拟化平台,把一个完整的物理机划分成若干个虚拟机来跑,从而实现资源的充分利用. Openstack对硬件的要求很高,要是你的物理机内存是4G的话,虚拟机的内存给 ...

  2. 第七章 设计程序架构 之 设计HTTP模块和处理程序

    1. 概述 HTTP模块和处理程序,可以让程序员直接跟HTTP请求交互. 本章内容包括 实现同步和异步模块及处理程序以及在IIS中如何选择模块和处理程序. 2. 主要内容 2.1 实现同步和异步模块及 ...

  3. 三行命令搞定查询Python安装目录

    想为Python添加一个库文件到默认目录,却忘记了Python安装目录. 其实,只要用下面三行命令,就可以轻松得到Python安装路径了. 进入Python >>>import sy ...

  4. 使用 Azure 创建存储和检索文件

    本指南将以循序渐进的方式帮助您使用 Azure 将文件存储到云中.我们将逐一介绍如何创建存储账户.创建容器.上传文件.检索文件和删除文件.在本教程中完成的所有操作均符合 1 元试用条件. 本指南将以循 ...

  5. SQLServer查询耗时sql语句

    qs.total_worker_time/qs.execution_count as [Avg CPU Time], , ( ) as query_text, qt.dbid, dbname=db_n ...

  6. strophe.js 插件 XMPP openfire

    参考资料:http://strophe.im/strophejs/ https://github.com/strophe/strophejs-plugins http://amazeui.org/ 最 ...

  7. NBUT 1118 Marisa's Affair (排序统计,水)

    题意: 每行给出一个人名和一个int值,人名可重复出现.要求对同一个人名统计int值,最大的先输出,若相同,则按照人名出现次数,若再相同,则按照人名字典序. 思路: 输入完全部进行排序,写个比较函数传 ...

  8. js 控制台输出

    var a = 'string'; var b = 123; console.log("The %s jumped over %d tall buildings", a, b); ...

  9. 数据库web项目对数据库的操作

    1.0.JSTL与jsp实现对数据库的操作 MySql 数据库: create database if not exists CommodityDB; use CommodityDB; drop ta ...

  10. PHP的PDF扩展库TCPDF将中文字体设置为内嵌字体的方法

    1. 下载要设置的字体,如名为simfang.ttf,放在./vendor/tecnickcom/tcpdf/tools目录中 2.在tools目录中按住shift,点击鼠标右键,点击“在此处打开命令 ...