POJ3691DNA repair

题解:

构建出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 }

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
时间: 2024-10-21 11:58:47

POJ3691DNA repair的相关文章

poj3691--DNA repair(AC自动机+dp)

DNA repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5743   Accepted: 2693 Description Biologists finally invent techniques of repairing DNA that contains segments causing kinds of inherited diseases. For the sake of simplicity, a

【poj3691-DNA repair】AC自动机+DP

题意:给n个病毒DNA序列,再给一个DNA序列,问该序列至少修改多少个碱基能不含任何病毒DNA.病毒DNA序列一共不超过1000,询问的DNA长度不超过1000. 题解:DP:d[l][p]表示询问到第l位.当前在AC自动机上的位置为p时的最少修改数,用d[l][p]推d[l+1][x].本来打的是递归,结果递归死循环了..于是改成递推了. 不想清楚的话递归真的好容易死掉TAT 1 //poj3691 2 #include<cstdio> 3 #include<cstdlib> 4

[POJ3253] Fence Repair

Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a s

poj 3253 Fence Repair

Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 40465   Accepted: 13229 Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000)

POJ 3253 Fence Repair (优先队列)

POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needsN (1 ≤ N ≤ 20,000) planks of wood, each having some integer lengthLi (1 ≤ Li ≤ 50,000) units. He the

HDU2124 Repair the Wall(贪心)

Problem Description Long time ago , Kitty lived in a small village. The air was fresh and the scenery was very beautiful. The only thing that troubled her is the typhoon. When the typhoon came, everything is terrible. It kept blowing and raining for

HDU 2124 Repair the Wall

Repair the Wall Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3076    Accepted Submission(s): 1504 Problem Description Long time ago , Kitty lived in a small village. The air was fresh and th

poj 3691 DNA repair(AC自动机+dp)

DNA repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5877   Accepted: 2760 Description Biologists finally invent techniques of repairing DNA that contains segments causing kinds of inherited diseases. For the sake of simplicity, a

POJ 3253-Fence Repair(贪心)

Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28417   Accepted: 9229 Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000)