HDU2457DNA repair(AC自动机+DP)

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个模板串,并且给你一个文本串,现在问你这个文本串最少需要改变几个字符才能使得它不包含任何模板串.(以上字符只由A,T,G,C构成).

现在我们令dp[i][j]=x表示当前在自动机的i号节点,且走了长为j的路了,且过程中都不经过单词节点的情况下,与文本串的前j个字符不同点最少有x个.(仔细体会dp[i][j]的定义).初值dp[i][j]=-1,dp[0][0]=0.dp[i][j] = min( dp[k][j-1] + v ) 从k可以走到i,且i和k都是非后缀单词. 设从k走到i的那条边的字符为x,如果x==s[j-1],那么v=0.否则v=1.

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<map>
#include<iomanip>
#include<math.h>
#include<sstream>
using namespace std;
typedef long long ll;
typedef double ld;
const int maxnode=1000+100;
const int sigma_size=4;
map<char,int> mp;
char sigma[4];
int N,M;//M为文本串的长度
char str[maxnode];//文本串
int trip[maxnode][sigma_size];
int f[maxnode];
int match[maxnode];
int dp[maxnode][maxnode];
int sz;
void init()
{
    sz=1;
    memset(trip[0],0,sizeof(trip[0]));
    match[0]=f[0]=0;
}
void add(char *s)
{
    int n=strlen(s),u=0;
    for(int i=0; i<n; i++)
    {
        int id=mp[s[i]];
        if(trip[u][id]==0)
        {
            trip[u][id]=sz;
            memset(trip[sz],0,sizeof(trip[sz]));
            match[sz++]=0;
        }
        u=trip[u][id];
    }
    match[u]=1;
}
void getFail()
{
    queue<int> q;
    for(int i=0; i<sigma_size; i++)
    {
        int u=trip[0][i];
        if(u)
        {
            f[u]=0;
            q.push(u);
        }
    }
    while(!q.empty())
    {
        int r=q.front();
        q.pop();
        for(int i=0; i<sigma_size; i++)
        {
            int u=trip[r][i];
            if(!u)
            {
                trip[r][i]=trip[f[r]][i];
                continue;
            }
            q.push(u);
            int v=f[r];
            while(v && trip[v][i]==0)
                v=f[v];
            f[u]=trip[v][i];
            match[u] |= match[f[u]];
        }
    }
}

int solve()
{
    int ans=-1;
    for(int i=0; i<sz; i++)
        for(int j=0; j<=M; j++)
            dp[i][j]=-1;
    dp[0][0]=0;
    for(int len=0; len<M; len++)
        for(int i=0; i<sz; i++)
            if(match[i]==0 && dp[i][len] != -1)
                for(int j=0; j<sigma_size; j++)
                    if(match[trip[i][j]]==0)
                    {
                        int k=trip[i][j];
                        int v=(str[len]==sigma[j])? 0:1;
                        if(dp[k][len+1] != -1)
                            dp[k][len+1] = min(dp[k][len+1], dp[i][len]+v);
                        else
                            dp[k][len+1] = dp[i][len]+v;
                        if(len+1==M)
                        {
                            if(ans != -1)
                                ans = min(ans, dp[k][len+1]);
                            else
                                ans = dp[k][len+1];
                        }
                    }
    return ans;
}

int main()
{
    mp['A']=0,mp['G']=1,mp['C']=2,mp['T']=3;
    sigma[0]='A',sigma[1]='G',sigma[2]='C',sigma[3]='T';
    int kase=0;
    while(scanf("%d",&N)==1&&N)
    {
        init();
        while(N--)
        {
            scanf("%s",str);
            add(str);
        }
        scanf("%s",str);
        M=strlen(str);
        getFail();
        printf("Case %d: %d\n",++kase,solve());
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值