题目链接:https://vjudge.net/problem/HDU-2457

DNA repair

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3126    Accepted Submission(s): 1661

Problem 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
 
Source

题意:

给出n个遗传病DNA序列,以及一个人体DNA序列,问至少修改多少个脱氧核苷酸,使得人体DNA序列不含遗传病?

题解:

1.将n个序列插入AC自动机。

2.设dp[i][j]为:处理到第i个字符,且当前状态为j(自动机上的状态)的最少修改数。

3.AC自动机实际上是一张有向图,如果要求字符串不含有自动机里面的病毒,那么字符串只能沿着自动机上的边走,当然需要去除病毒结点。所以状态转移:当字符串中的第i个字符与状态j的字符相同,那么dp[i+1][newj] = dp[i][j],否则dp[i+1][newj] = dp[i][j] + 1,dp[i+1][newj]取最小值即可。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e5;
const int MAXN = 1e3+; int Map[];
char M[];
int dp[MAXN][MAXN];
struct Trie
{
int sz, base;
int next[MAXN][], fail[MAXN], end[MAXN];
int root, L;
int newnode()
{
for(int i = ; i<sz; i++)
next[L][i] = -;
end[L++] = false;
return L-;
} void init(int _sz, int _base)
{
sz = _sz;
base = _base;
L = ;
root = newnode();
}
void insert(char buf[])
{
int len = strlen(buf);
int now = root;
for(int i = ; i<len; i++)
{
if(next[now][Map[buf[i]]] == -) next[now][Map[buf[i]]] = newnode();
now = next[now][Map[buf[i]]];
}
end[now] |= true;
}
void build()
{
queue<int>Q;
fail[root] = root;
for(int i = ; i<sz; i++)
{
if(next[root][i] == -) next[root][i] = root;
else fail[next[root][i]] = root, Q.push(next[root][i]);
}
while(!Q.empty())
{
int now = Q.front();
Q.pop();
end[now] |= end[fail[now]]; //当前串的后缀是否也包含单词
for(int i = ; i<sz; i++)
{
if(next[now][i] == -) next[now][i] = next[fail[now]][i];
else fail[next[now][i]] = next[fail[now]][i], Q.push(next[now][i]);
}
}
} int query(char s[])
{
int len = strlen(s);
for(int i = ; i<=len; i++)
for(int j = ; j<L; j++)
dp[i][j] = INF; dp[][] = ;
for(int i = ; i<len; i++)
for(int j = ; j<L; j++)
{
if(end[j] || dp[i][j]==INF) continue;
for(int k = ; k<sz; k++)
{
int newi = i+;
int newj = next[j][k];
if(end[newj]) continue;
dp[newi][newj] = min(dp[newi][newj], dp[i][j]+(s[i]!=M[k]));
}
} int ret = INF;
for(int i = ; i<L; i++)
ret = min(ret, dp[len][i]);
return ret==INF?-:ret;
}
}; Trie ac;
char buf[MAXN];
int main()
{
Map['A'] = ; Map['C'] = ; Map['G'] = ; Map['T'] = ; //离散化
M[] = 'A'; M[] = 'C'; M[] = 'G'; M[] = 'T';
int n, kase = ;
while(scanf("%d", &n) && n)
{
ac.init(,'A');
for(int i = ; i<=n; i++)
{
scanf("%s", buf);
ac.insert(buf);
}
ac.build();
scanf("%s", buf);
int ans = ac.query(buf);
printf("Case %d: %d\n", ++kase, ans);
}
return ;
}

最新文章

  1. stackoverfow访问 ajax.googleapis.com
  2. 协程、异步IO
  3. 前端安全之XSS攻击
  4. 【译】用boosting构建简单的目标分类器
  5. Node-restify 简介
  6. Android 5.0 双卡信息管理分析
  7. 省略nslog打印
  8. ios copy/strong/weak..使用总结
  9. smarty变量调节器案例
  10. Codeforces Round #278 (Div. 2)
  11. linux命令行下命令参数前的一横(-)和两横(--)的区别
  12. UIScrollView,UIView转换UIImage代码(整个view截图, 不只是可视区域)
  13. weapon制作武器
  14. java基础进阶:SQL的运用
  15. UGUI学习之InputField
  16. 2014.8.15模拟赛【公主的工作】&amp;&amp;bzoj1046[HAOI2007]上升序列
  17. 使用SVN clang: error: linker command failed with exit code 1 (use -v to see invocation)
  18. 使用EF连接Postgresql
  19. Android性能优化之加快应用启动速度
  20. PdfReader按页将PDF切割成多个PDF

热门文章

  1. mysql 远程登陆不上
  2. c语言单链表实现
  3. 知其一不知其二之Jenkins Hacking
  4. 路飞学城Python爬虫课第一章笔记
  5. Unique Binary Search Trees I&amp;II——给定n有多少种BST可能、DP
  6. JSON 值转换
  7. Android日期对话框NumberPicker的使用方法教程
  8. QTreeWidget 的用法
  9. phpstorm 设置
  10. ASP.NET数据库连接字符串的加密与解密