您的位置:首页 > 大数据 > 人工智能

HDU 2457 DNA repair

2015-11-03 23:10 507 查看

DNA repair

Time Limit: 2000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 2457
64-bit integer IO format: %I64d Java class name: Main

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

Source

2008 Asia Hefei Regional Contest Online by USTC

解题:AC自动机 + dp 其实是Trie图

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1010;
char str[maxn];
int dp[maxn][maxn],id[130],n,cs = 1;

struct Trie {
int ch[maxn][4],fail[maxn],tot;
bool flag[maxn];
int newnode() {
memset(ch[tot],0,sizeof ch[tot]);
fail[tot] = flag[tot] = 0;
return tot++;
}
void init() {
tot = 0;
newnode();
}
void insert(char *str,int root = 0){
for(int i = 0; str[i]; ++i){
if(!ch[root][id[str[i]]]) ch[root][id[str[i]]] = newnode();
root = ch[root][id[str[i]]];
}
flag[root] = true;
}
void build(int root = 0) {
queue<int>q;
for(int i = 0; i < 4; ++i) {
if(ch[root][i]) {
fail[ch[root][i]] = root;
q.push(ch[root][i]);
}
}
while(!q.empty()) {
root = q.front();
q.pop();
for(int i = 0; i < 4; ++i) {
int &nx = ch[root][i];
if(nx) {
fail[nx] = ch[fail[root]][i];
flag[nx] = flag[nx]||flag[fail[nx]];
q.push(nx);
} else nx = ch[fail[root]][i];
}
}
}
void solve(char *str) {
memset(dp,0x3f,sizeof dp);
int len = strlen(str);
dp[0][0] = 0;
for(int i = 0; i < len; ++i)
for(int j = 0; j < tot; ++j) {
if(dp[i][j] >= INF) continue;
for(int k = 0; k < 4; ++k) {
int nx = ch[j][k];
if(flag[nx]) continue;
dp[i+1][nx] = min(dp[i+1][nx],dp[i][j] + (id[str[i]] != k));
}
}
int ret = INF;
for(int i = 0; i < tot; ++i)
ret = min(ret,dp[len][i]);
printf("Case %d: %d\n",cs++,ret == INF?-1:ret);
}
} ac;
int main() {
for(int i = 0; i < 4; ++i) id["ATCG"[i]] = i;
while(scanf("%d",&n),n) {
ac.init();
for(int i = 0; i < n; ++i) {
scanf("%s",str);
ac.insert(str);
}
ac.build();
scanf("%s",str);
ac.solve(str);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: