码迷,mamicode.com
首页 > 其他好文 > 详细

POJ3691DNA repair

时间:2014-12-20 23:28:19      阅读:346      评论:0      收藏:0      [点我收藏+]

标签:

题解:

构建出trie图,令f[i][j]表示到第i个字符走到j号节点最少需要修改的字符数,然后枚举后继节点转移即可。

代码:没写caseWA了n发。。。

技术分享
  1 #include<cstdio>
  2 #include<cstdlib>
  3 #include<cmath>
  4 #include<cstring>
  5 #include<algorithm>
  6 #include<iostream>
  7 #include<vector>
  8 #include<map>
  9 #include<set>
 10 #include<queue>
 11 #include<string>
 12 #define inf 1000000000
 13 #define maxn 2000+5
 14 #define maxm 500+100
 15 #define eps 1e-10
 16 #define ll long long
 17 #define pa pair<int,int>
 18 #define for0(i,n) for(int i=0;i<=(n);i++)
 19 #define for1(i,n) for(int i=1;i<=(n);i++)
 20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
 21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
 22 #define for4(i,x) for(int i=head[x],y;i;i=e[i].next)
 23 #define mod 1000000007
 24 using namespace std;
 25 inline int read()
 26 {
 27     int x=0,f=1;char ch=getchar();
 28     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
 29     while(ch>=0&&ch<=9){x=10*x+ch-0;ch=getchar();}
 30     return x*f;
 31 }
 32 int n,tot,f[maxn][maxn],t[maxn][5],go[maxn];
 33 char s[maxn];
 34 bool v[maxn];
 35 queue<int>q;
 36 inline int id(char ch)
 37 {
 38     if(ch==A)return 0;
 39     if(ch==G)return 1;
 40     if(ch==C)return 2;
 41     if(ch==T)return 3;
 42 }
 43 inline void insert()
 44 {
 45     memset(s,0,sizeof(s));
 46     scanf("%s",s+1);int len=strlen(s+1),now=1;
 47     for1(i,len)
 48     {
 49         int x=id(s[i]);
 50         if(!t[now][x])t[now][x]=++tot;
 51         now=t[now][x];
 52     }
 53     v[now]=1;
 54 }
 55 void bfs()
 56 {
 57     q.push(1);
 58     while(!q.empty())
 59     {
 60         int x=q.front(),y,j;v[x]|=v[go[x]];q.pop();
 61         for0(i,3)
 62         {
 63             j=go[x];
 64             while(j&&!t[j][i])j=go[j];
 65             if(t[x][i])
 66             {
 67                 go[y=t[x][i]]=j?t[j][i]:1;
 68                 q.push(y);
 69             }else t[x][i]=j?t[j][i]:1;
 70         }
 71     }
 72 }
 73 int main()
 74 {
 75     freopen("input.txt","r",stdin);
 76     freopen("output.txt","w",stdout);
 77     int cs=0;
 78     while(cin>>n&&n)
 79     {
 80         tot=1;
 81         memset(t,0,sizeof(t));
 82         memset(v,0,sizeof(v));
 83         memset(go,0,sizeof(go));
 84         for1(i,n)insert();
 85         bfs();
 86         memset(s,0,sizeof(s));
 87         scanf("%s",s+1);n=strlen(s+1);
 88         for0(i,n)for1(j,tot)f[i][j]=inf;
 89         f[0][1]=0;
 90         for0(i,n-1)
 91          for1(j,tot)
 92           if(f[i][j]!=inf)
 93            for0(k,3)
 94             if(!v[t[j][k]]) 
 95              f[i+1][t[j][k]]=min(f[i+1][t[j][k]],f[i][j]+(k!=id(s[i+1])));
 96         int ans=inf;
 97         for1(i,tot)ans=min(ans,f[n][i]);
 98         printf("Case %d: %d\n",++cs,ans==inf?-1:ans);
 99     }
100     return 0;
101 }
View Code

 

DNA repair
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 5712   Accepted: 2677

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

POJ3691DNA repair

标签:

原文地址:http://www.cnblogs.com/zyfzyf/p/4175970.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!