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 ...
随机推荐
- 长安铃木经销商爬取(解析xml、post提交、python中使用js代码)
1.通过火狐浏览器,查找大长安铃木官网中关于经销商的信息主要在两个网页中 http://www.changansuzuki.com/khfw/xml/pro.xml 地域信息 http://www. ...
- 字符串做异或使用union
#include <stdio.h> #include <sys/time.h> #include <string.h> union data { unsigned ...
- 解决rhel相关系统下yum找不到安装包的解决方法
最近重新安装了Linux,用的版本是CentOS 5.1.但老是出现很多包找不到的情况. [root@toughhou /]# yum install rlwrap Loaded plugins: f ...
- 【go】脑补框架 Express beego tornado Flux reFlux React jsx jpg-ios出品
http://goexpresstravel.com/ 今天 Express 的作者 TJ Holowaychuk 发了一篇文章,正式宣告和 Node.js 拜拜了,转向 Go 语言. Go vers ...
- Hibernate从入门到精通(七)多对一单向关联映射
上次的博文Hibernate从入门到精通(六)一对一双向关联映射中我们介绍了一下一对一双向关联映射,本次博文我们讲解一下多对一关联映射 多对一单向关联映射 多对一关联映射与一对一关联映射类似,只是在多 ...
- 【BZOJ 2878】 [Noi2012]迷失游乐园
Description 放假了,小Z觉得呆在家里特别无聊,于是决定一个人去游乐园玩.进入游乐园后,小Z看了看游乐园的地图,发现可以将游乐园抽象成有n个景点.m条道路的无向连通图,且该图中至多有一个环( ...
- speed up your sharepoint
1. warm up http://blog.nowan.hu/post/SPWakeUp-Wake-up-your-SharePoint-quickly http://blogs.msdn.com/ ...
- poj 2728 Desert King (最小比例生成树)
http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissio ...
- AnimateWindow 阻塞当前线程问题
今天查了蛮多的,虽然不是系统的学习,收获也不小.下面说一下我的解决方法: 问题一:采用 AnimateWindow API实现消息窗体FormMsg的动画出现,但由于该方法会阻塞当前线程,造成其他窗体 ...
- 1047: [HAOI2007]理想的正方形 - BZOJ
Description 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小.Input 第一行为3个整数,分别表示a,b,n的值第二行至第a ...