题解:

构建出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 ;
}
DNA repair
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 input consists of multiple test cases. Each test case starts with a line containing one integers N (1 ≤ N ≤ 50), which is the number of DNA segments causing inherited diseases.
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

For each test case, print a line containing the test case number( beginning with 1) followed by the
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的更多相关文章

  1. 【poj3691-DNA repair】AC自动机+DP

    题意:给n个病毒DNA序列,再给一个DNA序列,问该序列至少修改多少个碱基能不含任何病毒DNA.病毒DNA序列一共不超过1000,询问的DNA长度不超过1000. 题解:DP:d[l][p]表示询问到 ...

  2. 理解 OpenStack + Ceph (9): Ceph 的size/min_size/choose/chooseleaf/scrubbing/repair 等概念

    本系列文章会深入研究 Ceph 以及 Ceph 和 OpenStack 的集成: (1)安装和部署 (2)Ceph RBD 接口和工具 (3)Ceph 物理和逻辑结构 (4)Ceph 的基础数据结构 ...

  3. poj 3253 Fence Repair

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 42979   Accepted: 13999 De ...

  4. 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 ...

  5. 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 ...

  6. poj3253 Fence Repair

    http://poj.org/problem?id=3253 Farmer John wants to repair a small length of the fence around the pa ...

  7. mysql下优化表和修复表命令使用说明(REPAIR TABLE和OPTIMIZE TABLE)

    随着mysql的长期使用,可以修复表来优化,优化时减少磁盘占用空间.方便备份. REPAIR TABLE `table_name` 修复表 OPTIMIZE TABLE `table_name` 优化 ...

  8. 执行查询报: Incorrect key file for table ‘test’; try to repair it

    报错信息如下:程序没有错误,但执行会报错,错误定在执行语句上 查了一下资料 网上的解决办法,亲试可用: mysql> use news;Database changedmysql> rep ...

  9. computer repair services in Hangzhou

    We provide support for all kinds of Windows based Desktops and Laptops all over Hangzhou,I will be i ...

随机推荐

  1. 跟着PHP100第一季学写一个CMS(1-10)

    笔记: 这次用的方法是先跟着视频做一遍,隔一天或半天后独立再做一遍,能发现真正不会的地方记录下来. CMS0.1界面布局1.问题:分两个css来实现时basic.css+index.php出现定位不正 ...

  2. envi中多波段图层叠加layer stacking

    Basic Tools——layer stacking 选择投影和输出的文件 波段1-7波段图层都叠加在一个文件中了

  3. mvc简单execl导出

    直接上代码: public static byte[] GetExecl(DataTable dt, List<string> list) { var sbHtml = new Strin ...

  4. kruskal算法-Pascal

    马上就快要考试了,然而突然发现自己图论已经废了,于是再都打一遍练练手...... const maxn=; maxe=maxn*maxn; type edge=record //edge记录每一条边, ...

  5. (转载)Cocos2dx-OpenGL ES2.0教程:使用VBO索引(4)

    在上一篇文章中,我们介绍了uniform和模型-视图-投影变换,相信大家对于OpenGL ES 2.0应该有一点感觉了.在这篇文章中,我们不再画三角形了,改为画四边形.下篇教程,我们就可以画立方体了, ...

  6. vc编程时说“Cannot open include file: 'unistd.h': No such file or directory”

    本文专自http://blog.csdn.net/mangobar/article/details/6314700 unistd.h是unix standard header之意,因此,Linux下开 ...

  7. bnuoj 29375 Two Strings(字符串?)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=29375 [题意]:可以对两字符串进行如下操作: 1.可以无损耗交换相邻两个字符(可以理解成交换任意字 ...

  8. XML工具类 - XmlUtils.java

    XML工具类,提供序列化XML.反序列化XML.获取指定节点的值的方法. 源码如下:(点击下载 - XmlUtils.java.dom4j-1.6.1.jar.xstream-1.4.7.jar ) ...

  9. FileFilter

    FileFilter 下面的例子中我们创建了一个FileFilter类,此类根据文件名的扩展名是否为.png来筛选文件.创建FileFilter实例之后需要将此实例作为参数传给File的listFil ...

  10. css3实现非矩形图片效果

    经常在网站上看到有一些非矩形的图片展示.在以前可能我会毫不犹豫的直接放上张处理好的图片.但是这样的话确实有些不太友好.每每需要换图的时候,都要去开图像处理软件也是蛮拼的.自从有了css3的选装,妈妈就 ...