描述
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.
输入
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.
输出
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.
样例输入
2 AAA AAG AAAG 2 A TG TGAATG 4 A G C T AGT 0
样例输出
Case 1: 1 Case 2: 4 Case 3: -1
1 #include <iostream> 2 #include <string.h> 3 #include <algorithm> 4 #include <stack> 5 #include <string> 6 #include <math.h> 7 #include <queue> 8 #include <stdio.h> 9 #include <string.h> 10 #include <vector> 11 #include <fstream> 12 #include <set> 13 #define inf 0x3f3f3f3f 14 15 using namespace std; 16 const int maxn = 1005; 17 int n,nodecou,l; 18 char str[maxn]; 19 int gen[85],f[maxn][maxn]; 20 21 struct node { 22 int next[4]; 23 int prev; 24 bool isdanger; 25 char val; 26 node() { 27 memset(next, 0, sizeof(next)); 28 prev = -1; 29 isdanger = false; 30 val = 0; 31 } 32 }tree[maxn]; 33 34 void insert() { 35 int now = 1,l=strlen(str); 36 for (int i = 0; i < l; i++) { 37 int idx = gen[str[i]]; 38 if (tree[now].next[idx] == 0) { 39 nodecou++; 40 tree[now].next[idx] = nodecou; 41 tree[nodecou].val = str[i]; 42 } 43 now = tree[now].next[idx]; 44 if (i == l - 1) 45 tree[now].isdanger = true; 46 } 47 } 48 49 void build() { 50 for(int i=0;i<4;i++) 51 tree[0].next[i] = 1; 52 tree[1].prev = 0; 53 queue<int>q; 54 q.push(1); 55 while (!q.empty()) { 56 int now = q.front(); q.pop(); 57 for (int i = 0; i < 4; i++) { 58 int child = tree[now].next[i]; 59 int prev = tree[now].prev; 60 if (child) { 61 while (tree[prev].next[i] == NULL) 62 prev = tree[prev].prev; 63 tree[child].prev = tree[prev].next[i]; 64 if (tree[tree[child].prev].isdanger) 65 tree[child].isdanger = true; 66 q.push(child); 67 } 68 else { 69 while (!tree[prev].next[i]) 70 prev = tree[prev].prev; 71 tree[now].next[i] = tree[prev].next[i]; 72 if (tree[child].isdanger) 73 tree[now].next[i] = 0; 74 } 75 } 76 } 77 } 78 79 void dp(int kase) { 80 for (int i = 1; i <= l; i++) { 81 for (int j = 1; j <= nodecou; j++) 82 { 83 for (int k = 0; k < 4; k++) 84 if (tree[j].next[k] && !tree[tree[j].next[k]].isdanger) 85 f[i][tree[j].next[k]] = min(f[i - 1][j] + (gen[str[i-1]]!=k), f[i][tree[j].next[k]]); 86 } 87 } 88 int ans = inf; 89 for (int i = 1; i <= nodecou; i++) 90 ans = min(ans, f[l][i]); 91 if (ans < inf) 92 printf("Case %d: %d\n", kase, ans); 93 else 94 printf("Case %d: -1\n", kase); 95 } 96 97 void init() { 98 int kase = 0; 99 while (scanf("%d",&n)) { 100 nodecou = 1; 101 if (n == 0)return; 102 kase++; 103 memset(tree, 0, sizeof(tree)); 104 memset(f, 0, sizeof(f)); 105 while (n--) 106 { 107 scanf("%s", str); 108 insert(); 109 } 110 build(); 111 scanf("%s", str); 112 l = strlen(str); 113 for (int i = 0; i <= l; i++) 114 for (int j = 1; j <= nodecou; j++) 115 f[i][j] = inf; 116 f[0][1] = 0; 117 dp(kase); 118 } 119 } 120 121 int main() 122 { 123 gen[‘A‘] = 0, gen[‘G‘] = 1, gen[‘C‘] = 2, gen[‘T‘] = 3; 124 init(); 125 return 0; 126 }
原文地址:https://www.cnblogs.com/yalphait/p/9874064.html