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

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自动机,并按套路标记所有危险节点(单词结点)
d p [ i ] [ j ] dp[i][j] dp[i][j]表示修改完DNA的前 i i i位,且当前在AC自动机的 j j j节点上 的 最少修改次数

初始化 d p [ 0 ] [ 0 ] = 0 dp[0][0]=0 dp[0][0]=0,其余为INF
S [ i + 1 ] = = c h [ i ] [ k ] S[i+1]==ch[i][k] S[i+1]==ch[i][k],则 d p [ i + 1 ] [   c h [ j ] [ k ]   ] = m i n ( d p [ i + 1 ] [   c h [ j ] [ k ]   ] , d p [ 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 ] ! = c h [ i ] [ k ] S[i+1]!=ch[i][k] S[i+1]!=ch[i][k],则 d p [ i + 1 ] [   c h [ j ] [ k ]   ] = m i n ( d p [ i + 1 ] [   c h [ j ] [ k ]   ] , d p [ 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)

最后找到 d p [ l e n ] [ 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;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ - 3616是一个题目,题目描述如下: 给定一组区间,每个区间有一个权重,要求选择一些区间,使得这些区间的右端点都小于等于k,并且权重之和最大。请问最大的权重和是多少? 解决这个问题的思路是使用动态规划。首先,将区间按照左端点从小到大进行排序。然后,定义一个dp数组,dp[i]表示右端点小于等于i的所有区间所能得到的最大权重。 接下来,遍历每一个区间,对于每个区间i,将dp[i]初始化为区间i的权重。然后,再遍历i之前的每个区间j,如果区间j的右端点小于等于k,并且区间j的权重加上区间i的权重大于dp[i],则更新dp[i]为dp[j]加上区间i的权重。 最后,遍历整个dp数组,找到最大的权重和,即为所求的答案。 下面是具体的代码实现: ```cpp #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct interval{ int start, end, weight; }; interval intervals[10005]; int dp[10005]; int n, m, k; bool compare(interval a, interval b) { if (a.start == b.start) { return a.end < b.end; } else { return a.start < b.start; } } int main() { while(~scanf("%d %d %d", &n, &m, &k)) { memset(dp, 0, sizeof dp); for (int i = 0; i < m; i++) { scanf("%d %d %d", &intervals[i].start, &intervals[i].end, &intervals[i].weight); } sort(intervals, intervals + m, compare); for (int i = 0; i < m; i++) { dp[i] = intervals[i].weight; for (int j = 0; j < i; j++) { if (intervals[j].end <= k && dp[j] + intervals[i].weight > dp[i]) { dp[i] = dp[j] + intervals[i].weight; } } } int maxWeight = 0; for (int i = 0; i < m; i++) { maxWeight = max(maxWeight, dp[i]); } printf("%d\n", maxWeight); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值