Blackops

初心易得,始终难守

0%

HDU 2457 DNA repair(AC自动机+DP)

DNA repair

Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2789 Accepted Submission(s): 1493

Problem 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

题目链接:HDU 2457
题意就是将长度为$l$的字符串修改为不包含任何给定字符串的最少修改的字符个数,可以在AC自动机上进行DP,$dp[i][j]$表示当前的字符串长度为$i$,走到了AC自动机上第$j$个节点,不包含任意给定字符串的最少修改的字符个数;然后考虑下一步,肯定不能走到危险节点处,因此对于$j$的下一步安全节点$v$,显然有

最后从$dp[len]$安全节点选出最小的修改数就是答案了
代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 1010;
struct Trie
{
int nxt[4];
int fail, cnt;
void init()
{
for (int i = 0; i < 4; ++i)
nxt[i] = -1;
fail = cnt = 0;
}
} L[N];
int sz, M;
char s[N];
int id[N], dp[N][N];

namespace AC
{
void init()
{
sz = 0;
L[sz++].init();
}
void ins(char s[], int len)
{
int u = 0;
for (int i = 0; i < len; ++i)
{
int v = id[s[i]];
if (L[u].nxt[v] == -1)
{
L[sz].init();
L[u].nxt[v] = sz++;
}
u = L[u].nxt[v];
}
L[u].cnt = 1;
}
void build()
{
queue<int>Q;
L[0].fail = 0;
for (int i = 0; i < 4; ++i)
{
if (L[0].nxt[i] == -1)
L[0].nxt[i] = 0;
else
{
L[L[0].nxt[i]].fail = 0;
Q.push(L[0].nxt[i]);
}
}
while (!Q.empty())
{
int u = Q.front();
Q.pop();
int uf = L[u].fail;
if (L[uf].cnt)
L[u].cnt = 1;
for (int i = 0; i < 4; ++i)
{
if (L[u].nxt[i] == -1)
L[u].nxt[i] = L[uf].nxt[i];
else
{
L[L[u].nxt[i]].fail = L[uf].nxt[i];
Q.push(L[u].nxt[i]);
}
}
}
}
}
int main(void)
{
int n, i, j, k;
int T = 0;
id['A'] = 0;
id['T'] = 1;
id['G'] = 2;
id['C'] = 3;
while (~scanf("%d", &n) && n)
{
AC::init();
for (i = 0; i < n; ++i)
{
scanf("%s", s);
AC::ins(s, strlen(s));
}
AC::build();
scanf("%s", s + 1);
int len = strlen(s + 1);
CLR(dp, INF);
dp[0][0] = 0;
for (i = 1; i <= len; ++i)
{
for (j = 0; j < sz; ++j)
{
for (k = 0; k < 4; ++k)
{
int v = L[j].nxt[k];
if (!L[v].cnt)
dp[i][v] = min(dp[i - 1][j] + (id[s[i]] == k ? 0 : 1), dp[i][v]);
}
}
}
int ans = INF;
for (i = 0; i < sz; ++i)
if (!L[i].cnt)
ans = min(dp[len][i], ans);
printf("Case %d: %d\n", ++T, ans == INF ? -1 : ans);
}
return 0;
}