POJ - 3691 DNA repair【AC自动机+DP】

本文介绍了一种通过修改最少数量的字符来消除DNA中导致遗传疾病的片段的技术。使用AC自动机构建病毒序列,并进行动态规划以找到最有效的修复路径。

Time limit 2000 ms
Memory limit 65536 kB

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.


题目大意

给定n个病毒序列和一个DNA序列,求最少改动DNA序列的多少个字符使得任何一个病毒序列都不是它的子串,若无法做到则输出-1


题目分析

用所有病毒序列构造出AC自动机,并按套路标记所有危险节点(单词结点)
dp[i][j]dp[i][j]dp[i][j]表示修改完DNA的前iii位,且当前在AC自动机的jjj节点上 的 最少修改次数

初始化dp[0][0]=0dp[0][0]=0dp[0][0]=0,其余为INF
S[i+1]==ch[i][k]S[i+1]==ch[i][k]S[i+1]==ch[i][k],则dp[i+1][ ch[j][k] ]=min(dp[i+1][ ch[j][k] ],dp[i][j])dp[i+1][\ ch[j][k]\ ]=min(dp[i+1][\ ch[j][k]\ ],dp[i][j])dp[i+1][ ch[j][k] ]=min(dp[i+1][ ch[j][k] ],dp[i][j])
S[i+1]!=ch[i][k]S[i+1]!=ch[i][k]S[i+1]!=ch[i][k],则dp[i+1][ ch[j][k] ]=min(dp[i+1][ ch[j][k] ],dp[i][j]+1)dp[i+1][\ ch[j][k]\ ]=min(dp[i+1][\ ch[j][k]\ ],dp[i][j]+1)dp[i+1][ ch[j][k] ]=min(dp[i+1][ ch[j][k] ],dp[i][j]+1)

最后找到dp[len][i]dp[len][i]dp[len][i]的最小值即可


#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long lt;
typedef double dd;

int read()
{
    int f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return f*x;
}

const int inf=1128481603;
const int maxn=2010;
int n,cnt,cs;
int ch[maxn][5],rem[maxn];
int fail[maxn],dp[maxn][maxn];
queue<int> q;
char pos[]="ACGT";
char pt[maxn],txt[maxn];

void init()
{
	cnt=0;
	memset(ch,0,sizeof(ch));
	memset(rem,0,sizeof(rem));
	memset(dp,67,sizeof(dp));
	memset(fail,0,sizeof(fail));
}

int get(char c)
{
	for(int i=0;i<4;++i)
	if(pos[i]==c) return i;
}

void ins(char* ss,int len)
{
    int u=0;
    for(int i=0;i<len;++i)
    {
        int x=get(ss[i]);
        if(!ch[u][x]) ch[u][x]=++cnt;
        u=ch[u][x];
    }
    rem[u]=1;
}

void ACM()
{
    for(int i=0;i<4;++i)
    if(ch[0][i]) fail[ch[0][i]]=0,q.push(ch[0][i]);

    while(!q.empty())
    {
        int u=q.front(); q.pop();
        for(int i=0;i<4;++i)
        {
            if(!ch[u][i]) ch[u][i]=ch[fail[u]][i];
            else{
                fail[ch[u][i]]=ch[fail[u]][i];
                rem[ch[u][i]]|=rem[fail[ch[u][i]]];
                q.push(ch[u][i]);
            }
        }
    }
}

int DP()
{
	int len=strlen(txt+1);
	dp[0][0]=0;
	for(int i=0;i<len;++i)
	for(int j=0;j<=cnt;++j)
	{
		if(dp[i][j]==inf) continue;
		for(int k=0;k<4;++k)
		{
			if(rem[ch[j][k]]) continue;
			dp[i+1][ch[j][k]]=min(dp[i+1][ch[j][k]],dp[i][j]+(txt[i+1]!=pos[k]));
		}
	}
	
	int ans=inf;
	for(int i=0;i<=cnt;++i)
	ans=min(ans,dp[len][i]);
	return ans==inf?-1:ans;
}

int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		if(n==0) break; init();
		for(int i=1;i<=n;++i)
		{
			scanf("%s",&pt);
			ins(pt,strlen(pt));
		}
		ACM();
		scanf("%s",txt+1);
		printf("Case %d: %d\n",++cs,DP());
	}
	return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值