http://poj.org/problem?id=3691

http://acm.hdu.edu.cn/showproblem.php?pid=2457

DNA repair
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5690   Accepted: 2669

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个模式串和一个文本串,问最少改动文本串中多少个字母使得文本串中不包括模式串。

分析:

N个模式串构建AC自己主动机,然后文本串在AC自己主动机中走,当中单词结点不可达。

用dp[i][j]表示文本串第i个字母转移到AC自己主动机第j个结点最少改动字母的个数,状态转移方程为dp[i][j]=min(dp[i][j],dp[i-1][last]+add),last表示j的前趋,add为当前点是否改动。因为第i个仅仅和第i-1个有关,所以能够使用滚动数组来优化空间。

/*
*
* Author : fcbruce <fcbruce8964@gmail.com>
*
* Time : Tue 18 Nov 2014 11:17:49 AM CST
*
*/
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10 #ifdef _WIN32
#define lld "%I64d"
#else
#define lld "%lld"
#endif #define maxm
#define maxn 1024 using namespace std; int q[maxn]; const int maxsize = 4;
struct Acauto
{
int ch[maxn][maxsize];
bool val[maxn];
int last[maxn],nex[maxn];
int sz;
int dp[2][maxn]; Acauto()
{
memset(ch[0],0,sizeof ch[0]);
val[0]=false;
sz=1;
} void clear()
{
memset(ch[0],0,sizeof ch[0]);
val[0]=false;
sz=1;
} int idx(const char c)
{
if (c=='A') return 0;
if (c=='T') return 1;
if (c=='C') return 2;
return 3;
} void insert(const char *s)
{
int u=0;
for (int i=0;s[i]!='\0';i++)
{
int c=idx(s[i]);
if (ch[u][c]==0)
{
memset(ch[sz],0,sizeof ch[sz]);
val[sz]=false;
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]=true;
} void get_fail()
{
int f=0,r=-1;
nex[0]=0;
for (int c=0;c<maxsize;c++)
{
int u=ch[0][c];
if (u!=0)
{
nex[u]=0;
q[++r]=u;
last[u]=0;
}
} while (f<=r)
{
int x=q[f++];
for (int c=0;c<maxsize;c++)
{
int u=ch[x][c];
if (u==0)
{
ch[x][c]=ch[nex[x]][c];
continue;
}
q[++r]=u;
int v=nex[x];
nex[u]=ch[v][c];
val[u]|=val[nex[u]];
}
}
} int DP(const char *T)
{
memset(dp,0x3f,sizeof dp);
dp[0][0]=0;
int x=1;
for (int i=0;T[i]!='\0';i++,x^=1)
{
memset(dp[x],0x3f,sizeof dp[x]);
int c=idx(T[i]);
for (int j=0;j<sz;j++)
{
if (dp[x^1][j]==INF) continue;
for (int k=0;k<4;k++)
{
if (val[ch[j][k]]) continue;
int add=k==c?0:1;
dp[x][ch[j][k]]=min(dp[x][ch[j][k]],dp[x^1][j]+add);
}
}
} int MIN=INF;
for (int i=0;i<sz;i++)
MIN=min(MIN,dp[x^1][i]);
if (MIN==INF) MIN=-1;
return MIN;
}
}acauto; char DNA[1024]; int main()
{
#ifdef FCBRUCE
freopen("/home/fcbruce/code/t","r",stdin);
#endif // FCBRUCE int n,__=0; while (scanf("%d",&n),n!=0)
{
acauto.clear();
for (int i=0;i<n;i++)
{
scanf("%s",DNA);
acauto.insert(DNA);
} acauto.get_fail(); scanf("%s",DNA); printf("Case %d: %d\n",++__,acauto.DP(DNA));
} return 0;
}

POJ 3691 &amp; HDU 2457 DNA repair (AC自己主动机,DP)的更多相关文章

  1. Hdu 2457 DNA repair (ac自己主动机+dp)

    题目大意: 改动文本串的上的字符,使之不出现上面出现的串.问最少改动多少个. 思路分析: dp[i][j]表示如今 i 个字符改变成了字典树上的 j 节点. 然后顺着自己主动机一直转移方程. 注意合法 ...

  2. POJ 2778 DNA Sequence (AC自己主动机 + dp)

    DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...

  3. HDU - 2825 Wireless Password(AC自己主动机+DP)

    Description Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless ne ...

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

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

  5. HDU 2457 DNA repair(AC自动机+DP)题解

    题意:给你几个模式串,问你主串最少改几个字符能够使主串不包含模式串 思路:从昨天中午开始研究,研究到现在终于看懂了.既然是多模匹配,我们是要用到AC自动机的.我们把主串放到AC自动机上跑,并保证不出现 ...

  6. poj 3691 DNA repair(AC自己主动机+dp)

    DNA repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5877   Accepted: 2760 Descri ...

  7. Hdu 3341 Lost&#39;s revenge (ac+自己主动机dp+hash)

    标题效果: 举个很多种DNA弦,每个字符串值值至1.最后,一个长字符串.要安排你最后一次另一个字符串,使其没事子值和最大. IDEAS: 首先easy我们的想法是想搜索的!管她3721..直接一个字符 ...

  8. HDU 2825 Wireless Password (AC自己主动机,DP)

    pid=2825">http://acm.hdu.edu.cn/showproblem.php? pid=2825 Wireless Password Time Limit: 2000 ...

  9. HDU 2896 病毒侵袭 (AC自己主动机)

    pid=2896">http://acm.hdu.edu.cn/showproblem.php?pid=2896 病毒侵袭 Time Limit: 2000/1000 MS (Java ...

随机推荐

  1. (转)Log4J日志配置详解

    http://www.cnblogs.com/ITtangtang/p/3926665.html 一.Log4j简介 Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源 ...

  2. JSP 和 Servlet 有哪些相同点和不同点, 他们之间的联系是什么?

    jsp和servlet的区别和联系:1.jsp经编译后就变成了Servlet.(JSP的本质就是Servlet,JVM只能识别java的类,不能识别JSP的代码,Web容器将JSP的代码编译成JVM能 ...

  3. [转] iOS多线程编程之NSOperation和NSOperationQueue的使用

    <iOS多线程编程之NSThread的使用> 介绍三种多线程编程和NSThread的使用,这篇介绍NSOperation的使用. 使用 NSOperation的方式有两种, 一种是用定义好 ...

  4. IO调度算法研究1

    linux kernel 2.6之后提供了四种IO调度算法,每种调度算法都有其不同的特点和应用场景,系统使用者可以通过系统提供的接口,选择使用哪种IO调度算法,以及调整IO调度算法的参数,以达到最优的 ...

  5. 【转】使用Boost Graph library(二)

    原文转自:http://shanzhizi.blog.51cto.com/5066308/942972 让我们从一个新的图的开始,定义一些属性,然后加入一些带属性的顶点和边.我们将给出所有的代码,这样 ...

  6. 调试存储过程时提示ORA-20000: ORU-10027: buffer overflow

    下午的时候在 PL/SQl Developer 10.0.5.1710 上调试壹個存储过程,在调试的时候使用了比较多的 DBMS_OUTPUT.PUT_LINE 作为打印日志的方式,结果没过多久 PL ...

  7. Win32 SecuritySetting

    http://flylib.com/books/en/2.21.1.207/1/ http://blogs.technet.com/b/heyscriptingguy/archive/2011/11/ ...

  8. 如何让Table显示滚动条

    Table显示滚动条,要先把table放到一个div中,div的长度和宽度要固定,控制overflow属性为scroll <div style="width:700px; height ...

  9. Python之美[从菜鸟到高手]--一步一步动手给Python写扩展(异常处理和引用计数)

    我们将继续一步一步动手给Python写扩展,通过上一篇我们学习了如何写扩展,本篇将介绍一些高级话题,如异常,引用计数问题等.强烈建议先看上一篇,Python之美[从菜鸟到高手]--一步一步动手给Pyt ...

  10. 【Android病毒分析报告】 - ZxtdPay 吸费恶魔

    本文章由Jack_Jia编写,转载请注明出处.  文章链接:http://blog.csdn.net/jiazhijun/article/details/11581543 作者:Jack_Jia    ...