Lost's revenge

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2262    Accepted Submission(s): 565

Problem 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
 
Author
Qinz@XDU
 
Source
 
Recommend
lcy
 
 
 
 
字符最长是40
只需要记录ACGT出现的次数。
 
如果使用5维数组,显然超内存了。
 
假设ACGT的总数分别为num[0],num[1],num[2],num[3]
 
那么对于ACGT的数量分别为ABCD的状态可以记录为:
 
A*(num[1]+1)*(num[2]+1)*(num[3]+1) + B*(num[2]+1)*(num[3]+1)+ C*(num[3]+1) +D
 
这样的状态最大就是11*11*11*11
 
复杂度也可以承受了。
 
 
字符串可能有重复的,用int来记录数量
 
 
//============================================================================
// Name : HDU.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================ #include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <queue>
using namespace std;
const int INF = 0x3f3f3f3f;
struct Trie
{
int next[][],fail[];
int end[];
int root,L;
int newnode()
{
for(int i = ;i < ;i++)
next[L][i] = -;
end[L++] = ;
return L-;
}
void init()
{
L = ;
root = newnode();
}
int getch(char ch)
{
if(ch == 'A')return ;
else if(ch == 'C')return ;
else if(ch == 'G')return ;
else return ;
}
void insert(char buf[])
{
int len = strlen(buf);
int now = root;
for(int i = ;i < len;i++)
{
if(next[now][getch(buf[i])] == -)
next[now][getch(buf[i])] = newnode();
now = next[now][getch(buf[i])];
}
end[now] ++;
}
void build()
{
queue<int>Q;
fail[root] = root;
for(int i = ;i < ;i++)
if(next[root][i] == -)
next[root][i] = root;
else
{
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
while(!Q.empty())
{
int now = Q.front();
Q.pop();
end[now] += end[fail[now]];/********/
for(int i = ;i < ;i++)
if(next[now][i] == -)
next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]] = next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
int dp[][***+];
int bit[];
int num[];
int solve(char buf[])
{
int len = strlen(buf);
memset(num,,sizeof(num));
for(int i = ;i < len;i++)
num[getch(buf[i])]++;
bit[] = (num[]+)*(num[]+)*(num[]+);
bit[] = (num[]+)*(num[]+);
bit[] = (num[]+);
bit[] = ;
memset(dp,-,sizeof(dp));
dp[root][] = ;
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 s = A*bit[] + B*bit[] + C*bit[] + D*bit[];
for(int i = ;i < L;i++)
if(dp[i][s] >= )
{
for(int k = ;k < ;k++)
{
if(k == && A == num[])continue;
if(k == && B == num[])continue;
if(k == && C == num[])continue;
if(k == && D == num[])continue;
dp[next[i][k]][s+bit[k]] = max(dp[next[i][k]][s+bit[k]],dp[i][s]+end[next[i][k]]);
}
}
}
int ans = ;
int status = num[]*bit[] + num[]*bit[] + num[]*bit[] + num[]*bit[];
for(int i = ;i < L;i++)
ans = max(ans,dp[i][status]);
return ans;
}
};
char buf[];
Trie ac;
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n;
int iCase = ;
while( scanf("%d",&n) == && n)
{
iCase++;
ac.init();
for(int i = ;i < n;i++)
{
scanf("%s",buf);
ac.insert(buf);
}
ac.build();
scanf("%s",buf);
printf("Case %d: %d\n",iCase,ac.solve(buf));
}
return ;
}
 
 
 
 
 
 
 
 
 
 
 

HDU 3341 Lost's revenge(AC自动机+DP)的更多相关文章

  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 2457 DNA repair(AC自动机+DP)题解

    题意:给你几个模式串,问你主串最少改几个字符能够使主串不包含模式串 思路:从昨天中午开始研究,研究到现在终于看懂了.既然是多模匹配,我们是要用到AC自动机的.我们把主串放到AC自动机上跑,并保证不出现 ...

  3. HDU 2425 DNA repair (AC自动机+DP)

    DNA repair Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. HDU 4758 Walk Through Squares(AC自动机+DP)

    题目链接 难得出一个AC自动机,我还没做到这个题呢...这题思路不难想,小小的状压出一维来,不过,D和R,让我wa死了,AC自动机,还得刷啊... #include<iostream> # ...

  5. [HDU 4787] GRE Words Revenge (AC自动机)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4787 题目大意: 给你若干个单词,查询一篇文章里出现的单词数.. 就是被我水过去的...暴力重建AC自 ...

  6. HDU 2825 Wireless Password【AC自动机+DP】

    给m个单词,由这m个单词组成的一个新单词(两个单词可以重叠包含)长度为n,且新单词中包含的基本单词数目不少于k个.问这样的新单词共有多少个? m很小,用二进制表示新单词中包含基本单词的情况. 用m个单 ...

  7. HDU3341 Lost's revenge(AC自动机&&dp)

    一看到ACGT就会想起AC自动机上的dp,这种奇怪的联想可能是源于某道叫DNA什么的题的. 题意,给你很多个长度不大于10的小串,小串最多有50个,然后有一个长度<40的串,然后让你将这个这个长 ...

  8. hdu3341Lost's revenge(ac自动机+dp)

    链接 类似的dp省赛时就做过了,不过这题卡内存,需要把当前状态hash一下,可以按进制来算出当前的状态,因为所有的状态数是不会超过10*10*10*10的,所以完全可以把这些存下来. 刚开始把trie ...

  9. HDU-3341-Lost's revenge(AC自动机, DP, 压缩)

    链接: https://vjudge.net/problem/HDU-3341 题意: Lost and AekdyCoin are friends. They always play "n ...

  10. HDU 6086 Rikka with String AC自动机 + DP

    Rikka with String Problem Description As we know, Rikka is poor at math. Yuta is worrying about this ...

随机推荐

  1. C#之系统自带保存属性

    源代码下载链接 程序开发很多时候需要根据运行环境做不通的参数配置,通过写ini之类的文本文件是一种方法,但这种方法也同时会把数据暴露 Winform开发中可以将需要配置的字段属性保存到程序中(其实也是 ...

  2. C# webservice 编写、发布、调用

    采用的工具VS2010生成工程 1. 生成webservice工程:建 ASP.NET 空WEB 应用程序. 2. 在建好的ASP.NET 空WEB应用程序中新建项“web 服务”. 完成上述内容工程 ...

  3. Appium 客户端库 API

    ## Appium 客户端库 Appium 有对应以下语言的客户端库: 语言 | 代码 :--|--:[Ruby][rubygems] | [GitHub](https://github.com/ap ...

  4. Log4perl 的使用

    Perl 使用Log4perl 首先下载log4 module : http://search.cpan.org/CPAN/authors/id/M/MS/MSCHILLI/Log-Log4perl- ...

  5. ePass.CreateFile

    javascript和vbscript中没有结构体Struct,ePass的ActiveX对象中把各个参数都展开了,官方文档只给出了对应的代码,没有给出相应的数字,示例代码中却都是数字,其VC代码中有 ...

  6. Pooled Allocation池式分配实例——Keil 内存管理

    最近翻看Kei安装目录,无意中发现C51\LIB下的几个.C文件: CALLOC.CFREE.CINIT_MEM.CMALLOC.CREALLOC.C 看到 MALLOC.C 和 FREE.C 想到可 ...

  7. Effective C++ -----条款49:了解new-handler 的行为

    set_new_handler允许客户指定一个函数,在内存分配无法获得满足时被调用. Nothorw new 是一个颇为局限的工具,因为它只适用于内存分配:后继的构造函数调用还是可能抛出异常.

  8. FZU 2165 v11(最小重复覆盖)+ codeforces 417D Cunning Gena

    告诉你若干个(<=100)武器的花费以及武器能消灭的怪物编号,问消灭所有怪物(<=100)的最小花费...当然每个武器可以无限次使用,不然这题就太水了╮(╯▽╰)╭ 这题当时比赛的时候连题 ...

  9. 浅析 Cordova for iOS

    转自@夏小BO的技术博客: Cordova,对这个名字大家可能比较陌生,大家肯定听过 PhoneGap 这个名字,Cordova 就是 PhoneGap 被 Adobe 收购后所改的名字.(Cordo ...

  10. 关于Hibernate的关联映射

    何为关联映射 由于数据库的表与表之间存在的管理关系,可以分为一对一,一对多和多对多关联,一般情况下,在数据库设计中是通过表的外键来建立各种关系的,在Hibernate中则把数据库表与表之间的关系数据映 ...