HDU 2457:DNA repair

本文介绍了一种帮助生物学家修复含有致病基因片段的DNA算法。该算法通过改变最少数量的字符来消除所有致病基因片段,同时确保修复后的DNA只包含合法的碱基。

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
 
 
dp乱跑
#include<iostream>
#include<queue>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int LO=4,NU=1005;
inline int f(char u){
    if (u=='A') return 0;else
    if (u=='C') return 1;else
    if (u=='G') return 2;else
    if (u=='T') return 3;
}
struct tree{
    int f;
    bool w;
    int t[LO];
    int v[LO];
}t[NU];
int n,m,p,num;
bool us[NU];
char s[10000];
queue <int> q;
int dp[1005][NU];
inline bool dfs(int x){
    if (x==0) return 0;
    if (t[x].w) return 1;
    if (us[x]) return t[x].w;
    us[x]=1;
    return t[x].w|=dfs(t[x].f);
}
inline void in(){
    int p=0,l,m=strlen(s);
    for (register int i=0;i<m;i++){
        l=f(s[i]);
        if (!t[p].t[l]) t[p].t[l]=++num;
        p=t[p].t[l];
    }
    t[p].w=1;
}
inline void mafa(){
    register int i;int k,p;
    q.push(0);t[0].f=0;
    while(!q.empty()){
        k=q.front();q.pop();
        for (i=0;i<LO;i++)
        if (t[k].t[i]){
            p=t[k].f;
            while((!t[p].t[i])&&p) p=t[p].f;
            t[t[k].t[i]].f=(k==p)?0:t[p].t[i];
            q.push(t[k].t[i]);
        }
    }
}
const int INF=1e9;
int main(){
    int tt=0;
    register int i,j,k,l;int u;int ans;
    for(;;){
        tt++;
        scanf("%d",&n);
        if (!n) return 0;
        num=u=ans=0;
        for (i=0;i<n;i++) scanf("%s",s),in();
        mafa();
        for (i=0;i<=num;i++) us[i]=0;
          for (i=0;i<=num;i++)
        t[i].w|=dfs(i);
        for (i=0;i<=num;i++)
        for (j=0;j<LO;j++){
            if (!t[i].t[j]){
                u=t[i].f;
                while(!t[u].t[j]&&u) u=t[u].f;
                u=t[u].t[j];
            }else u=t[i].t[j];
            t[i].v[j]=u;
        }
        scanf("%s",s);
        n=strlen(s);
        for (i=0;i<=n;i++)
        for (j=0;j<=num;j++) dp[i][j]=INF;
        dp[0][0]=0;
        for (i=0;i<n;i++)
        for (j=0;j<=num;j++)
        if (dp[i][j]!=INF)
        for (l=0;l<LO;l++)
        if (!t[t[j].v[l]].w)
        if (dp[i+1][t[j].v[l]]>dp[i][j]+(l==f(s[i])?0:1))
        dp[i+1][t[j].v[l]]=dp[i][j]+(l==f(s[i])?0:1);
        ans=INF;
        for (j=0;j<=num;j++) if (dp[n][j]<ans) ans=dp[n][j];
        if (ans==INF) ans=-1;
        printf("Case %d: %d\n",tt,ans);
        for (i=0;i<=num;i++)
        for (j=0;j<LO;j++) t[i].t[j]=t[i].v[j]=0;
        for (i=0;i<=num;i++) t[i].w=t[i].f=0;
    }
}
View Code

 

 

转载于:https://www.cnblogs.com/Enceladus/p/5312284.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值