HDU3341 Lost's revenge(AC自动机+DP)
题目是给一个DNA重新排列使其包含最多的数论基因。
考虑到内存大概就只能这么表示状态:
dp[i][A][C][G][T],表示包含各碱基个数为ACGT且当前后缀状态为自动机第i的结点的字符串最多的数论基因数
其中ACGT可以hash成一个整数(a*C*G*T+c*G*T+g*T+T),这样用二维数组就行了,而第二维最多也就11*11*11*11个。
接下来转移依然是我为人人型,我是丢进一个队列,用队列来更新状态的值。
这题果然挺卡常数的,只好手写队列,最后4500msAC,还是差点超时,代码也搞得挺乱的。
#include<cstdio>
#include<cstring>
using namespace std;
int tn,ch[][],fail[],flag[];
int idx[];
void insert(char *s){
int x=;
for(int i=; s[i]; ++i){
int y=idx[s[i]];
if(ch[x][y]==) ch[x][y]=++tn;
x=ch[x][y];
}
++flag[x];
}
int que[];
void init(){
memset(fail,,sizeof(fail));
int front=,rear=;
for(int i=; i<; ++i){
if(ch[][i]) que[rear++]=ch[][i];
}
while(front!=rear){
int x=que[front++];
for(int i=; i<; ++i){
if(ch[x][i]) que[rear++]=ch[x][i],fail[ch[x][i]]=ch[fail[x]][i],flag[ch[x][i]]+=flag[ch[fail[x]][i]];
else ch[x][i]=ch[fail[x]][i];
}
}
}
int d[][];
int quex[*],quey[*];
int main(){
idx['A']=; idx['C']=; idx['G']=; idx['T']=;
char str[];
int n,cse=;
while(~scanf("%d",&n) && n){
tn=;
memset(ch,,sizeof(ch));
memset(flag,,sizeof(flag));
while(n--){
scanf("%s",str);
insert(str);
}
init();
scanf("%s",str);
int times[]={};
for(int i=; str[i]; ++i){
++times[idx[str[i]]];
}
int tcal0=(times[]+)*(times[]+)*(times[]+);
int tcal1=(times[]+)*(times[]+);
int tcal2=(times[]+);
memset(d,-,sizeof(d));
d[][]=;
int front=,rear=,x,y,ny,cnt[];
quex[rear]=; quey[rear]=; ++rear;
while(front!=rear){
x=quex[front]; y=quey[front]; ++front;
ny=y;
cnt[]=ny/tcal0;
ny-=cnt[]*tcal0;
cnt[]=ny/tcal1;
ny-=cnt[]*tcal1;
cnt[]=ny/tcal2;
cnt[]=ny-cnt[]*tcal2;
for(int i=; i<; ++i){
if(cnt[i]>=times[i]) continue;
++cnt[i];
ny=cnt[]*tcal0+cnt[]*tcal1+cnt[]*tcal2+cnt[];
if(d[ch[x][i]][ny]==-){
d[ch[x][i]][ny]=d[x][y]+flag[ch[x][i]];
quex[rear]=ch[x][i]; quey[rear]=ny; ++rear;
}else if(d[ch[x][i]][ny]<d[x][y]+flag[ch[x][i]]){
d[ch[x][i]][ny]=d[x][y]+flag[ch[x][i]];
}
--cnt[i];
}
}
y=times[]*tcal0+times[]*tcal1+times[]*tcal2+times[];
int res=;
for(int x=; x<=tn; ++x){
if(res<d[x][y]) res=d[x][y];
}
printf("Case %d: %d\n",++cse,res);
}
return ;
}
HDU3341 Lost's revenge(AC自动机+DP)的更多相关文章
- HDU3341 Lost's revenge(AC自动机&&dp)
一看到ACGT就会想起AC自动机上的dp,这种奇怪的联想可能是源于某道叫DNA什么的题的. 题意,给你很多个长度不大于10的小串,小串最多有50个,然后有一个长度<40的串,然后让你将这个这个长 ...
- 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 ...
- HDU-3341-Lost's revenge(AC自动机, DP, 压缩)
链接: https://vjudge.net/problem/HDU-3341 题意: Lost and AekdyCoin are friends. They always play "n ...
- hdu3341Lost's revenge(ac自动机+dp)
链接 类似的dp省赛时就做过了,不过这题卡内存,需要把当前状态hash一下,可以按进制来算出当前的状态,因为所有的状态数是不会超过10*10*10*10的,所以完全可以把这些存下来. 刚开始把trie ...
- POJ1625 Censored!(AC自动机+DP)
题目问长度m不包含一些不文明单词的字符串有多少个. 依然是水水的AC自动机+DP..做完后发现居然和POJ2778是一道题,回过头来看都水水的... dp[i][j]表示长度i(在自动机转移i步)且后 ...
- HDU2296 Ring(AC自动机+DP)
题目是给几个带有价值的单词.而一个字符串的价值是 各单词在它里面出现次数*单词价值 的和,问长度不超过n的最大价值的字符串是什么? 依然是入门的AC自动机+DP题..不一样的是这题要输出具体方案,加个 ...
- HDU2457 DNA repair(AC自动机+DP)
题目一串DNA最少需要修改几个基因使其不包含一些致病DNA片段. 这道题应该是AC自动机+DP的入门题了,有POJ2778基础不难写出来. dp[i][j]表示原DNA前i位(在AC自动机上转移i步) ...
- hdu 4117 GRE Words AC自动机DP
题目:给出n个串,问最多能够选出多少个串,使得前面串是后面串的子串(按照输入顺序) 分析: 其实这题是这题SPOJ 7758. Growing Strings AC自动机DP的进阶版本,主题思想差不多 ...
- hdu 2457(ac自动机+dp)
题意:容易理解... 分析:这是一道比较简单的ac自动机+dp的题了,直接上代码. 代码实现: #include<stdio.h> #include<string.h> #in ...
- HDU 2425 DNA repair (AC自动机+DP)
DNA repair Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- MySQL 5.6 Warning: Using a password on the command line interface can be insecure
MySQL 5.6 在命令行输入密码,就会提示这些安全警告信息. Warning: Using a password on the command line interface can be inse ...
- XSS的DOS攻击之 server limit dos
墨西哥同学周末很郁闷的在宾馆上网,发现youtube被ban了,于是写个了tool解决这个问题.顺带想到了一种利用 google 统计的漏洞,写在这里了 http://sirdarckcat.blog ...
- Class Methods & Variables
When calling an instance method like withdraw_securely, the syntax generally looks something like th ...
- Python Django 的 django templatedoesnotexist
django 1.8版本的解决方案 在 setting.py 这个文件里 TEMPLATES = [ ...... #原来的 #'DIRS': [ ], // 这个 列表里添加 template路 ...
- shell脚本调试之工具——bashdb
bash是Unix/Linux操作系统最常用的shell之一,它非常灵活,和awk.c++配合起来异常强大 以下使用一个测试脚本来说明使用bash调试的方法 test.sh #!/bin/bash e ...
- 查看别人的css
ie工具栏的“文件”选项选“另存为”到你本地电脑,存下来有两个文件 一个是空间名称命名的文件夹和html网页,文件加里有三个扩展名为.css的文件
- 在64位的linux上运行32位的程序
1.症状 (1)执行bin文件时提示:No such file or directory (2)ldd bin文件 的输出为: not a dynamic executable (3)file bi ...
- iOS 使用UIWebView把oc代码和javascript相关联
首先请参看一篇文章,作者写的很明白,请参看原地址 http://blog.163.com/m_note/blog/static/208197045201293015844274/. 其实,oc和js的 ...
- poj 1625 (AC自动机好模版,大数好模版)
题目 给n个字母,构成长度为m的串,总共有n^m种.给p个字符串,问n^m种字符串中不包含(不是子串)这p个字符串的个数. 将p个不能包含的字符串建立AC自动机,每个结点用val值来标记以当前节点为后 ...
- mybatis There is no getter for property named 'xx' in 'class java.lang.String
转载自://http://www.cnblogs.com/anee/p/3324140.html 用mybatis查询时,传入一个字符串传参数,且进行判断时,会报 There is no getter ...