POJ3691DNA repair
题解:
构建出trie图,令f[i][j]表示到第i个字符走到j号节点最少需要修改的字符数,然后枚举后继节点转移即可。
代码:没写caseWA了n发。。。
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 2000+5
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define for4(i,x) for(int i=head[x],y;i;i=e[i].next)
#define mod 1000000007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n,tot,f[maxn][maxn],t[maxn][],go[maxn];
char s[maxn];
bool v[maxn];
queue<int>q;
inline int id(char ch)
{
if(ch=='A')return ;
if(ch=='G')return ;
if(ch=='C')return ;
if(ch=='T')return ;
}
inline void insert()
{
memset(s,,sizeof(s));
scanf("%s",s+);int len=strlen(s+),now=;
for1(i,len)
{
int x=id(s[i]);
if(!t[now][x])t[now][x]=++tot;
now=t[now][x];
}
v[now]=;
}
void bfs()
{
q.push();
while(!q.empty())
{
int x=q.front(),y,j;v[x]|=v[go[x]];q.pop();
for0(i,)
{
j=go[x];
while(j&&!t[j][i])j=go[j];
if(t[x][i])
{
go[y=t[x][i]]=j?t[j][i]:;
q.push(y);
}else t[x][i]=j?t[j][i]:;
}
}
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int cs=;
while(cin>>n&&n)
{
tot=;
memset(t,,sizeof(t));
memset(v,,sizeof(v));
memset(go,,sizeof(go));
for1(i,n)insert();
bfs();
memset(s,,sizeof(s));
scanf("%s",s+);n=strlen(s+);
for0(i,n)for1(j,tot)f[i][j]=inf;
f[][]=;
for0(i,n-)
for1(j,tot)
if(f[i][j]!=inf)
for0(k,)
if(!v[t[j][k]])
f[i+][t[j][k]]=min(f[i+][t[j][k]],f[i][j]+(k!=id(s[i+])));
int ans=inf;
for1(i,tot)ans=min(ans,f[n][i]);
printf("Case %d: %d\n",++cs,ans==inf?-:ans);
}
return ;
}
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5712 | Accepted: 2677 |
Description
Biologists finally invent techniques of repairing DNA that contains segments causing kinds of inherited diseases. For the sake of simplicity, a DNA is represented as a string containing characters 'A', 'G' , 'C' and 'T'. The repairing techniques are simply to change some characters to eliminate all segments causing diseases. For example, we can repair a DNA "AAGCAG" to "AGGCAC" to eliminate the initial causing disease segments "AAG", "AGC" and "CAG" by changing two characters. Note that the repaired DNA can still contain only characters 'A', 'G', 'C' and 'T'.
You are to help the biologists to repair a DNA by changing least number of characters.
Input
The following N lines gives N non-empty strings of length not greater than 20 containing only characters in "AGCT", which are the DNA segments causing inherited disease.
The last line of the test case is a non-empty string of length not greater than 1000 containing only characters in "AGCT", which is the DNA to be repaired.
The last test case is followed by a line containing one zeros.
Output
number of characters which need to be changed. If it's impossible to repair the given DNA, print -1.
Sample Input
2
AAA
AAG
AAAG
2
A
TG
TGAATG
4
A
G
C
T
AGT
0
Sample Output
Case 1: 1
Case 2: 4
Case 3: -1
POJ3691DNA repair的更多相关文章
- 【poj3691-DNA repair】AC自动机+DP
题意:给n个病毒DNA序列,再给一个DNA序列,问该序列至少修改多少个碱基能不含任何病毒DNA.病毒DNA序列一共不超过1000,询问的DNA长度不超过1000. 题解:DP:d[l][p]表示询问到 ...
- 理解 OpenStack + Ceph (9): Ceph 的size/min_size/choose/chooseleaf/scrubbing/repair 等概念
本系列文章会深入研究 Ceph 以及 Ceph 和 OpenStack 的集成: (1)安装和部署 (2)Ceph RBD 接口和工具 (3)Ceph 物理和逻辑结构 (4)Ceph 的基础数据结构 ...
- poj 3253 Fence Repair
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42979 Accepted: 13999 De ...
- How repair disk issue when "Fsck Failed please repair manually and reboot"
" Fsck Failed please repair manually and reboot. the root filesystem is currently mounted as re ...
- java.sql.SQLException: Incorrect key file for table 'C:\Windows\TEMP\#sql578_6e2_68d.MYI'; try to repair it
java.sql.SQLException: Incorrect key file for table 'C:\Windows\TEMP\#sql578_6e2_68d.MYI'; try to re ...
- poj3253 Fence Repair
http://poj.org/problem?id=3253 Farmer John wants to repair a small length of the fence around the pa ...
- mysql下优化表和修复表命令使用说明(REPAIR TABLE和OPTIMIZE TABLE)
随着mysql的长期使用,可以修复表来优化,优化时减少磁盘占用空间.方便备份. REPAIR TABLE `table_name` 修复表 OPTIMIZE TABLE `table_name` 优化 ...
- 执行查询报: Incorrect key file for table ‘test’; try to repair it
报错信息如下:程序没有错误,但执行会报错,错误定在执行语句上 查了一下资料 网上的解决办法,亲试可用: mysql> use news;Database changedmysql> rep ...
- computer repair services in Hangzhou
We provide support for all kinds of Windows based Desktops and Laptops all over Hangzhou,I will be i ...
随机推荐
- unity3d应用内分享(微信、微博等)的实现
问题:如何在unity3d的游戏中实现分享功能,如图 思路: 1.分享功能的实现方式有多种,较方便快捷的一种是直接调用android的API来调出系统的分享界面 2.unity3d里面调用androi ...
- Security log is full,only administrator can log on to fix the problem(安全日志满了)
When you login the system and see this error “Security log on this system is full,only administrato ...
- Treimu更新记录1.2.9.0
Treimu是一个WPF音乐播放器个人小项目.程序集文件:http://pan.baidu.com/s/1pJLSHsB项目源代码:http://pan.baidu.com/s/1jGHtjfC 1. ...
- linux crontab 命令
Linux 系统提供了使用者控制计划任务的命令 :crontab 命令. 一.crond简介 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划 ...
- gulp前端自动化构建工具入门篇
现在我们通过这3个问题来学习一下: 1.什么是gulp? 2.为什么要用gulp? 3.怎么用? 什么是gulp 答:是一个前端自动化的构建工具,直白点说,如果没有这个工具,我们利用人工依旧可以做 ...
- 退出telnet
telnet时,很多时候通过ctrl+c依然无法退出.可以采取下面的方式进行退出: ctrl+],然后进入 telnet>,然后输入q或quit即可.
- EXTJS API
EXTJS API 链接: http://docs.sencha.com/extjs/5.0.0/ http://docs.sencha.com/extjs/4.2.2/ http://docs.se ...
- js模拟触发事件
html标签元素封装着实用的[事件],但在很多时候,需要[模拟触发事件],比如 [按钮单机事件] 可以实实在在点击按钮触发该事件,但体验而言,很多时候需要js逻辑处理让实现 触发事件的效果这时就用 ...
- IE9下Coolite.Ext出现createContextualFragment错误
解决Ext在IE9上报错“createContextualFragment”,只需要在使用Coolite.Ext页面加入如下代码即可: if ((typeof Range !== "unde ...
- hdu 4712 Hamming Distance(随机函数暴力)
http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...