【HDU3341】 Lost's revenge (AC自动机+状压DP)
Lost's revenge
Time Limit: 5000MS Memory Limit: 65535KB 64bit IO Format: %I64d & %I64u 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
3ACCGGTCGAT1AAAAA0Sample Output
Case 1: 3
Case 2: 2
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 510
#define Maxl 15000
#define INF 0xfffffff
#define Mod 20090717 int n;
char s[];
int v[],k[],ln; struct node
{
// int cnt;
int fail,mark;
int son[];
}t[Maxn];int tot;
int num;
int p[]; void upd(int x)
{
// t[x].cnt=0;
t[x].mark=;
memset(t[x].son,,sizeof(t[x].son));
} int mymin(int x,int y) {return x<y?x:y;}
int mymax(int x,int y) {return x>y?x:y;} void read_trie()
{
scanf("%s",s+);
int len=strlen(s+);
int now=;
for(int i=;i<=len;i++)
{
if(s[i]=='C') s[i]='B';
else if(s[i]=='G') s[i]='C';
else if(s[i]=='T') s[i]='D';
int ind=s[i]-'A'+;
if(!t[now].son[ind])
{
t[now].son[ind]=++tot;
upd(tot);
}
now=t[now].son[ind];
if(i==len) t[now].mark++;
}
} queue<int > q;
void build_AC()
{
while(!q.empty()) q.pop();
q.push();
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=;i<=;i++)
{
if(t[x].son[i])
{
t[t[x].son[i]].fail=x?t[t[x].fail].son[i]:;
q.push(t[x].son[i]);
}
else t[x].son[i]=t[t[x].fail].son[i];
}
t[x].mark+=t[t[x].fail].mark;
}
} int f[Maxn][Maxl];
int a[];
void dp()
{
memset(f,-,sizeof(f));
f[][]=;
int ans=;
for(int l=;l<=ln;l++) //选取的字母个数
for(int i=;i<=tot;i++) //走到节点i
for(a[]=mymax(l-v[]-v[]-v[],);a[]<=mymin(v[],l);a[]++)//if(f[i-1][j][l]!=0)
for(a[]=mymax(l-v[]-v[]-a[],);a[]<=mymin(v[],l);a[]++)
for(a[]=mymax(l-v[]-a[]-a[],);a[]<=mymin(v[],l);a[]++)
{
a[]=l-a[]-a[]-a[];
int now=a[]*k[]+a[]*k[]+a[]*k[]+a[];
if(f[i][now]==-) continue;
for(int j=;j<=;j++) if(a[j]+<=v[j])
{
// if(t[t[i].son[j]].mark)
f[t[i].son[j]][now+k[j]]=mymax(f[t[i].son[j]][now+k[j]],f[i][now]+t[t[i].son[j]].mark);
// else f[t[i].son[j]][now+k[j]]=mymax(f[t[i].son[j]][now+k[j]],f[i][now]);
ans=mymax(ans,f[t[i].son[j]][now+k[j]]);
}
}
printf("%d\n",ans);
} char ss[];
void init()
{
tot=;
upd();
for(int i=;i<=n;i++)
{
read_trie();
}
build_AC();
scanf("%s",ss+);
ln=strlen(ss+);
v[]=v[]=v[]=v[]=;
for(int i=;i<=ln;i++)
{
if(ss[i]=='C') ss[i]='B';
else if(ss[i]=='G') ss[i]='C';
else if(ss[i]=='T') ss[i]='D';
v[ss[i]-'A'+]++;
}
int maxx=(v[]+)*(v[]+)*(v[]+)*(v[]+);
k[]=(v[]+)*(v[]+)*(v[]+);
k[]=(v[]+)*(v[]+);
k[]=(v[]+);k[]=;
} int main()
{
int kase=;
while()
{
scanf("%d",&n);
if(n==) break;
init();
printf("Case %d: ",++kase);
dp();
}
return ;
}
[HDU3341]
2016-07-12 09:42:49
【HDU3341】 Lost's revenge (AC自动机+状压DP)的更多相关文章
- hdu 2825 aC自动机+状压dp
Wireless Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- BZOJ1559 [JSOI2009]密码 【AC自动机 + 状压dp】
题目链接 BZOJ1559 题解 考虑到这是一个包含子串的问题,而且子串非常少,我们考虑\(AC\)自动机上的状压\(dp\) 设\(f[i][j][s]\)表示长度为\(i\)的串,匹配到了\(AC ...
- HDU 3247 Resource Archiver(AC自动机 + 状压DP + bfs预处理)题解
题意:目标串n( <= 10)个,病毒串m( < 1000)个,问包含所有目标串无病毒串的最小长度 思路:貌似是个简单的状压DP + AC自动机,但是发现dp[1 << n][ ...
- zoj3545Rescue the Rabbit (AC自动机+状压dp+滚动数组)
Time Limit: 10 Seconds Memory Limit: 65536 KB Dr. X is a biologist, who likes rabbits very much ...
- hdu2825 Wireless Password(AC自动机+状压dp)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- hdu 4057--Rescue the Rabbit(AC自动机+状压DP)
题目链接 Problem Description Dr. X is a biologist, who likes rabbits very much and can do everything for ...
- hdu 6086 -- Rikka with String(AC自动机 + 状压DP)
题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...
- UVALive - 4126 Password Suspects (AC自动机+状压dp)
给你m个字符串,让你构造一个字符串,包含所有的m个子串,问有多少种构造方法.如果答案不超过42,则按字典序输出所有可行解. 由于m很小,所以可以考虑状压. 首先对全部m个子串构造出AC自动机,每个节点 ...
- 【hdu2825】ac自动机 + 状压dp
传送门 题目大意: 给你一些密码片段字符串,让你求长度为n,且至少包含k个不同密码片段串的字符串的数量. 题解: 因为密码串不多,可以考虑状态压缩 设dp[i][j][sta]表示长为i的字符串匹配到 ...
- HDU 2825 Wireless Password(AC自动机 + 状压DP)题解
题意:m个密码串,问你长度为n的至少含有k个不同密码串的密码有几个 思路:状压一下,在build的时候处理fail的时候要用 | 把所有的后缀都加上. 代码: #include<cmath> ...
随机推荐
- 常用命令-eval简析
www.2cto.com 1. 工作原理及用法 用法:eval command-line 原理:eval主要用在对参数的特殊处理上面的,一般的命令行,shell处理参数就只执行一遍,像转义和变 ...
- ubuntu 14.04/15.10 安装基于eclipse的android app开发环境
一开始是装了ubuntu15.10,不知道是我的x200机器太old还是iso镜像有问题,总是各种莫名的引导不起来.有时候刚刚装好的干净系统,只install了一个vim和openssh,重启,然后就 ...
- mysql select 语法
格式:select [选项子句] 字段表达式子句 [from子句] [where子句] [group by子句] [having子句] [order by子句] [limit子句]; 提示:子句的顺序 ...
- Bootstrap 开关(switch)控件需要注意的问题
远程文档地址:http://www.bootcss.com/p/bootstrap-switch/ 先上lz遇到的小坑:自古无图无真相的原则 上面代码注释掉后 就是下面这个图片效果!然后加载顺序也要注 ...
- PHP 正则通配符
$a = preg_match('/ph+p/','aaaphpbbbp'); +的前导就是h $a = preg_match('/ph+p/','aaaphhhhhhhhhhpbbbp'); //第 ...
- sharepoint中的YesNo字段
sharepoint中的YesNo字段实际上是一个Boolean字段,性格有点特别,如果IsShow是一个YesNo字段,使用caml查询的时候值为”1“(Yes)”0“(No),Item[IsSho ...
- .NET设计模式(6):原型模式(Prototype Pattern)
):原型模式(Prototype Pattern) ); //使用颜色 string colorName = "red"; C ...
- 组策略彻底解决windows 2003 终端数
win2003的话可以从组策略修改: 组策略级别要高于终端服务配置,当启用组策略后终端服务配置中的相应选项会变成灰色不可修改 运行-gpedit.msc-计算机配置-管理模板-Windows ...
- count(*),count(1)和count(主键) 区别
看如下数据: SQL> select count(*) from ysgl_compile_reqsub; COUNT(*) ---------- 已用时间: : : 07.51 SQL) fr ...
- 连接远程LINUX服务器
远程登陆linux服务器需要下载一个软件,非常好用,名字是SecureCRT5,百度搜索有很多,如果下载不到可以联系我 运行安装包,一路下一步就可以了 安装好后,运行该软件 点击左上角第二 ...