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. 如何用纯 CSS 创作一种侧立图书的特效

    效果预览 在线演示 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. https://codepen.io/zhang-ou/pen/deVgRM 可交互视频教程 此视 ...

  2. 如何在Python中显式释放内存?

    根据Python官方文档,您可以强制垃圾收集器释放未引用的内存gc.collect().例: import gc gc.collect() 所属网站分类: python高级 > 综合&其 ...

  3. LeetCode 467. Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  4. Device eth0 does not seem to be present,delaying initialization问题

    1.打开/etc/udev/rules.d/70-persistent-net.rules: cat /etc/udev/rules.d/70-persistent-net.rules 文件内容如图: ...

  5. idea 中使用 出现 svn: E155036

    在idea中使用svn  checkout时  svn出现如上错误. 原因本地的工作副本太旧.command line进入本地工作副本的根目录,执行svn upgrade后 重启idea就可以了.

  6. spring用到的设计模式

    https://www.cnblogs.com/yuefan/p/3763898.html https://www.cnblogs.com/hwaggLee/p/4510687.html https: ...

  7. CodeForces 21 A+B

                                                         Jabber ID 判断邮箱地址格式是否正确..一把心酸泪...跪11+,,看后台才过.. 注 ...

  8. hdu 1325数据弱

    #include<stdio.h>//判断是否有环,判断是否有点,判断是否是一个父节点 #include<string.h> #define N 1000000 int pre ...

  9. 会修修的莫队--BZOJ2120: 数颜色

    $n \leq 10000$的数列,$m \leq 10000$个操作,一:单点修改:二:查区间不同数字个数.修改数$\leq 1000$,数字$\leq 1000000$. 我不会告诉您这是三种写法 ...

  10. Codeforces917D. Stranger Trees

    $n \leq 100$的完全图,对每个$0 \leq K \leq n-1$问生成树中与给定的一棵树有$K$条公共边的有多少个,答案$mod \ \ 1e9+7$. 对这种“在整体中求具有某些特性的 ...