[HDU2457]DNA repair-AC自动机

来源:互联网 发布:vm虚拟机装mac 编辑:程序博客网 时间:2024/06/04 18:54

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


咱这才几天没写AC自动机啊(╯‵□′)╯︵┻━┻居然差点忘记了……
(虽然好像咱才写过一题的样子)

思路:
又是一道AC自动机DP~
但这题的DP并没有什么难想的地方……
相比之下,二小姐和恋恋的那题简直丧心病狂……
dp[i][j]表示在子串i位置,自动机上j号节点最少需要修改的次数,直接从当前节点往下对其每种碱基子节点转移即可,遇到一个结尾若当前走到的字符和结尾字符相同就要+1…..

然而咱这种手残党把好好的AC自动机硬是写成了CE自动机……还是连续两次.(╯‵□′)╯︵┻━┻
而且咱还对着样例的第三组数据苦思冥想为什么咱的自动机输出了-1,却没注意答案就是-1……

果然咱还是太弱了╮(╯▽╰)╭~

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespace std;const int N=1233;const int Inf=0x3f3f3f3f;struct AC_Automaton{    int next[N][4],fail[N];    int pool,l,r,q[N],dp[N][N];    bool end[N];    void init()    {        pool=0;        memset(next,0,sizeof(next));        memset(fail,0,sizeof(fail));        memset(end,0,sizeof(end));        memset(dp,0,sizeof(dp));    }    int num(char s)    {        switch(s)        {            case 'A':return 0;break;            case 'C':return 1;break;            case 'G':return 2;break;            case 'T':return 3;break;        }        return 0;    }    void add(char *s)    {        int len=strlen(s);        int now=0;        for(int i=0;i<len;i++)        {            if(!next[now][num(s[i])])                next[now][num(s[i])]=++pool;            now=next[now][num(s[i])];        }        end[now]=1;    }    void getfail()    {        q[r=1]=0;l=0;        fail[0]=-1;        while(l!=r)        {            int u=q[++l];            if(end[fail[u]])end[u]=1;//重要            for(int i=0;i<=3;i++)            {                if(next[u][i])                {                    q[++r]=next[u][i];                    fail[next[u][i]]= u==0?0:next[fail[u]][i];                }                   else                    next[u][i]= u==0?0:next[fail[u]][i];            }        }    }    int calc(char *s)    {        memset(dp,Inf,sizeof(dp));        dp[0][0]=0;        int len=strlen(s+1);        for(int i=1;i<=len;i++)            for(int j=0;j<=pool;j++)                if(dp[i-1][j]!=Inf)                    for(int k=0;k<=3;k++)                        if(!end[next[j][k]])                            dp[i][next[j][k]]=min(dp[i][next[j][k]],dp[i-1][j]+(num(s[i])!=k));        int ans=Inf;        for(int i=0;i<=pool;i++)            ans=min(ans,dp[len][i]);        if(ans==Inf)            return -1;        else return ans;    }}koishi;int main(){    int n,t=0;    char s[N];    while(scanf("%d",&n) && n)    {        koishi.init();        for(int i=1;i<=n;i++)        {            scanf("%s",s);            koishi.add(s);        }        koishi.getfail();        scanf("%s",s+1);        printf("Case %d: %d\n",++t,koishi.calc(s));    }}
0 0
原创粉丝点击