首页 > 其他 > 详细

HDU 2457:DNA repair

时间:2016-03-23 18:26:59      阅读:322      评论:0      收藏:0      [点我收藏+]

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

 

 

HDU 2457:DNA repair

原文:http://www.cnblogs.com/Enceladus/p/5312284.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!