DNA repair

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
 
题意:
  
  给你n个模式串,给出一个原串,问最少需要修改多少个字符,使得原串中不包含任何一个模式串
  
题解:
 
  将这个n个模式串建立AC自动机
  设定dp[i][j]表示在trie树上第j个节点的状态下能让前i个字符保持不包含任何一个模式串的最小修改量
  简单转移
 
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 5e5+, M = 1e3+, mod = 1e9+, inf = 1e9; int dp[][],sum[N],nex[N][],cnt,head,tail,q[N],fail[N]; int ID(char x) {
if(x == 'A') return ;
else if(x == 'T') return ;
else if(x == 'C') return ;
else return ;
}
void insert(char *s) {
int now = ,len = strlen(s);
for(int i = ; i < len; ++i) {
int index = ID(s[i]);
if(!nex[now][index])
nex[now][index] = ++cnt;
//sum[nex[now][index]] |= sum[now];
now = nex[now][index];
}
sum[now] = ;
} void build_fail() {
head = , tail = ;
for(int i = ; i < ; ++i) nex[][i] = ;
fail[] = ;
q[tail++] = ;
while(head!=tail) {
int now = q[head++];
sum[now] |= sum[fail[now]];
for(int i = ; i < ; ++i) {
int p = fail[now];
if(!nex[now][i]) {
nex[now][i] = nex[p][i];continue;
}
fail[nex[now][i]] = nex[p][i];
q[tail++] = nex[now][i];
}
}
}
int n;
char s[N],a[N];
int main() {
int cas = ;
while(scanf("%d",&n)!=EOF) {
if(n == ) break;
cnt = ;
memset(sum,,sizeof(sum));
memset(nex,,sizeof(nex));
memset(fail,,sizeof(fail));
for(int i = ; i <= n; ++i) {
scanf("%s",s);
insert(s);
}
build_fail();
scanf("%s",a+);
n = strlen(a+);
for(int i = ; i <= n; ++i)
for(int j = ; j <= cnt; ++j)
dp[i][j] = inf;
dp[][] = ;
for(int i = ; i < n; ++i) {
for(int j = ; j <= cnt; ++j) {
if(dp[i][j] == inf) continue;
for(int k = ; k < ; ++k) {
if(k == ID(a[i+])) {
if(!sum[nex[j][k]])dp[i+][nex[j][k]] = min(dp[i+][nex[j][k]],dp[i][j]);
}
else {
if(!sum[nex[j][k]]) dp[i+][nex[j][k]] = min(dp[i+][nex[j][k]],dp[i][j]+);
}
}
}
}
int ans = inf;
for(int i = ; i <= cnt; ++i) {
ans = min(dp[n][i],ans);
}
printf("Case %d: ",cas++);
if(ans == inf) puts("-1");
else printf("%d\n",ans);
}
return ;
}

HDU 2457/POJ 3691 DNA repair AC自动机+DP的更多相关文章

  1. POJ 3691 DNA repair(AC自动机+DP)

    题目链接 能AC还是很开心的...此题没有POJ2778那么难,那个题还需要矩阵乘法,两个题有点相似的. 做题之前,把2778代码重新看了一下,回忆一下当时做题的思路,回忆AC自动机是干嘛的... 状 ...

  2. HDU2457 DNA repair —— AC自动机 + DP

    题目链接:https://vjudge.net/problem/HDU-2457 DNA repair Time Limit: 5000/2000 MS (Java/Others)    Memory ...

  3. poj 2778 DNA Sequence AC自动机DP 矩阵优化

    DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11860   Accepted: 4527 Des ...

  4. [hdu2457]DNA repair(AC自动机+dp)

    题意:给出一些不合法的模式DNA串,给出一个原串,问最少需要修改多少个字符,使得原串中不包含非法串. 解题关键:多模式串匹配->AC自动机,求最优值->dp,注意在AC自动机上dp的套路. ...

  5. POJ 3691 DNA repair ( Trie图 && DP )

    题意 : 给出 n 个病毒串,最后再给出一个主串,问你最少改变主串中的多少个单词才能使得主串中不包含任何一个病毒串 分析 : 做多了AC自动机的题,就会发现这些题有些都是很套路的题目.在构建 Trie ...

  6. POJ3691 DNA repair(AC自动机 DP)

    给定N个长度不超过20的模式串,再给定一个长度为M的目标串S,求在目标串S上最少改变多少字符,可以使得它不包含任何的模式串 建立Trie图,求得每个节点是否是不可被包含的串,然后进行DP dp[i][ ...

  7. HDU 2457 DNA repair (AC自动机+DP)

    题意:给N个串,一个大串,要求在最小的改变代价下,得到一个不含上述n个串的大串. 思路:dp,f[i][j]代表大串中第i位,AC自动机上第j位的最小代价. #include<algorithm ...

  8. POJ 2778 DNA Sequence (AC自动机+DP+矩阵)

    题意:给定一些串,然后让你构造出一个长度为 m 的串,并且不包含以上串,问你有多少个. 析:很明显,如果 m 小的话 ,直接可以用DP来解决,但是 m 太大了,我们可以认为是在AC自动机图中,根据离散 ...

  9. HDU 6086 Rikka with String ——(AC自动机 + DP)

    这是一个AC自动机+dp的问题,在中间的串的处理可以枚举中断点来插入自动机内来实现,具体参见代码. 在这题上不止为何一直MLE,一直找不到结果(lyf相同写法的代码消耗内存较少),还好考虑到这题节点应 ...

随机推荐

  1. 条款21:必须返回对象时,别妄想返回其reference(Don't try to return a reference when you must return an object)

    NOTE: 1.绝不要返回pointer或reference 指向一个local stack 对象,或返回reference 指向一个heap-allocated对象,或返回pointer 或refe ...

  2. 07 mongodb

    mongodb mongodb简介 简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为Web应用提供可扩展的高性能数据存储解决方案.    MongoDB是一个介于关系数据 ...

  3. SpringMVC中controller的跳转

    controller中的重定向 (1)不需要传递参数重定向 方式一:使用ModelAndView        return new ModelAndView("redirect:/toLi ...

  4. Backspace doesn't delete inner html tags of a contenteditable DIV in Firefox

    https://bugzilla.mozilla.org/show_bug.cgi?id=439808 backspace键 在ff下不能使用  div contenteditable=true时

  5. python操作剪贴板错误提示:pywintypes.error: (1418, 'GetClipboardData',线程没有打开的剪贴板)

    问题现象:通过打断点,一步步调试可以正常复制和粘贴剪贴板数据.但是直接运行会报错pywintypes.error: (1418, 'GetClipboardData',线程没有打开的剪贴板) 问题原因 ...

  6. sql的case when用法

    select t.C_OPERATE_TIME MODIFY_TIME, t.c_code EMPLOYEE_CODE, t.c_name EMPLOYEE_NAME, CASE t.c_employ ...

  7. uva12558 Egyptian Fractions (HARD version)(迭代深搜)

    Egyptian Fractions (HARD version) 题解:迭代深搜模板题,因为最小个数,以此为乐观估价函数来迭代深搜,就可以了. #include<cstdio> #inc ...

  8. Codevs 2693 上学路线(施工)

    时间限制: 2 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题目描述 Description 问题描述 你所在的城市街道好像一个棋盘,有a条南北方向的街道和b条东西方向的街道. 南 ...

  9. 【POJ1149&BZOJ1280】PIGS(最大流)

    题意:Emmy在一个养猪场工作.这个养猪场有M个锁着的猪圈,但Emmy并没有钥匙. 顾客会到养猪场来买猪,一个接着一个.每一位顾客都会有一些猪圈的钥匙,他们会将这些猪圈打开并买走固定数目的猪. 所有顾 ...

  10. Visual Studio 2017 RC的坑

    ASP.NET Core Project add Docker Project Support的问题 执行上面操作以后,如果本机没有装好docker,就会一直报错,无法build通过,无论你在Proj ...