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. iptables:ipset批量管理ip

    1.安装 安装: yum -y install ipset \apt-get -y install ipset 2.创建一个ipset ipset create whitelist hash:net ...

  2. 【BZOJ 3555】 [Ctsc2014]企鹅QQ(哈希)

    Description PenguinQQ是中国最大.最具影响力的SNS(Social Networking Services)网站,以实名制为基础,为用户提供日志.群.即时通讯.相册.集市等丰富强大 ...

  3. python接口自动化-有token的接口项目使用unittest框架设计

    获取token 在做接口自动化的时候,经常会遇到多个用例需要用同一个参数token,并且这些测试用例跨.py脚本了. 一般token只需要获取一次就行了,然后其它使用unittest框架的测试用例全部 ...

  4. 72.spring boot讨论群【从零开始学Spring Boot】

    [从零开始学习Spirng Boot-常见异常汇总] 如果您碰到什么问题,您可以加群进行探讨,在群里有加入的都是Spring Boot志同道合的朋友: Spring Boot QQ交流群:193341 ...

  5. 使用using current logfile实现DG备库实时更新

    DG1是主库,DG2是备库:运行在最大可用模式. 实验一:未使用using current logfile参数时,备库在最大可用模式下,不能实时同步备库的更新 -此时需要等待主库进行归档---侧面证明 ...

  6. 洛谷 P2008 大朋友的数字

    DP,动态规划   树状数组   最长不下降子序列 by  GeneralLiu 题目 就是说给一串由 0~9 组成的序列 求 以 i (1~n) 结尾 的 最长不下降子序列 的 和 (最长不下降子序 ...

  7. LeetCode:不同路径&不同路径II

    不同路径一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“Finish”). 问 ...

  8. CodeForces - 43B Letter

    字符串的处理 统计已有字符的个数 和需求字符比较 #include <iostream> #include <stdio.h> #include <string.h> ...

  9. 2016 Multi-University Training Contest 2 solutions BY zimpha

    Acperience 展开式子, \(\left\| W-\alpha B \right\|^2=\displaystyle\alpha^2\sum_{i=1}^{n}b_i^2-2\alpha\su ...

  10. 寒武纪camp Day4

    补题进度:7/11 A(博弈论) 略 B 待填坑 C(贪心) 题意: 一个序列是good的当且仅当相邻两个数字不相同.给出一个长度为n的数列,每个数字是ai.定义一种操作就是把a中某个元素拿到首位去, ...