DNA repair POJ - 3691(AC自动机 + DP)

生物学家发明了新的技术,能够修复包含导致遗传疾病的DNA片段。本文介绍了一种算法,通过修改最少数量的字符来消除DNA中的致病片段,使用AC自动机进行高效匹配。

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

http://poj.org/problem?id=3691 

题目大意:给出单词表和一个文本串,问最少修改几个字符使得该文本串不包含所有的单词。

解题思路:先根据单词表构建一个AC自动机,具体匹配的时候我们可以定义一个状态dp[i][j]表示长度为i、以字典树中j号结点结尾的字符串不包含所有单词所需的最少修改次数。很容易递推发现,dp[i+1][u]也就是长度为1+1、以当前结点结尾的字符串的最小修改次数等于 u的所有孩子结点ch[j][k]是否和当前s[i]相等的最小值。

找了一个小时的BUG,就是因为在把C字符转换成对应的数字时,判断力写的小写c,真的是太不细心了!

/*
@Author: Top_Spirit
@Language: C++
*/
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <cmath>
using namespace std ;
typedef unsigned long long ull ;
typedef long long ll ;
typedef pair < int, int > P ;
const int Maxn = 60 * 30 ;
const int INF = 0x3f3f3f3f ;
const double PI = acos(-1.0) ;
const ull seed = 133 ;
const int _Max = 1000 + 10 ;

int Next[Maxn][4], fail[Maxn], val[Maxn], cnt ;
int dp[Maxn][Maxn], n ;
string str, str1 ;

inline int change (char ch){
    if (ch == 'A') return 0 ;
    if (ch == 'C') return 1 ;
    if (ch == 'G') return 2 ;
    if (ch == 'T') return 3 ;
}

void Insert(){
    int len = str.size() ;
    int root = 0 ;
    for (int i = 0; i < len; i++){
        int id = change(str[i]) ;
        if (!Next[root][id]) Next[root][id] = ++cnt ;
        root = Next[root][id] ;
    }
    val[root] = 1 ;
}

void getFail (){
    queue < int > que ;
    for (int i = 0; i < 4; i++){
        if (Next[0][i]) {
            fail[Next[0][i]] = 0 ;
            que.push(Next[0][i]) ;
        }
    }
    while (!que.empty()){
        int tmp = que.front() ;
        que.pop() ;
        if (val[fail[tmp]]) val[tmp] = 1 ;
        for (int i = 0; i < 4; i++){
            if (Next[tmp][i]){
                fail[Next[tmp][i]] = Next[fail[tmp]][i] ;
                que.push(Next[tmp][i]) ;
            }
            else Next[tmp][i] = Next[fail[tmp]][i] ;
        }
    }
}

void query(){
    int len = str1.size();
    for (int i = 0; i <= len; i++){
        for (int j = 0; j < cnt; j++){
            dp[i][j] = INF ;
        }
    }
    dp[0][0] = 0 ;
    for (int i = 0; i < len; i++){
        for (int j = 0; j < cnt; j++){
            if (dp[i][j] >= INF) continue ;
            for (int k = 0; k < 4; k++){
                int u = Next[j][k] ;
                if (val[u]) continue ;
                int tmp ;
                if (k == change(str1[i])) tmp = dp[i][j] ;
                else tmp = dp[i][j] + 1 ;
                dp[i + 1][u] = min(dp[i + 1][u], tmp) ;
            }
        }
    }
    int ans = INF ;
    for (int i = 0; i < cnt; i++) ans = min(dp[len][i], ans) ;
    if (ans == INF) cout << -1 << endl ;
    else cout << ans << endl ;
}

int main (){
    int Cas = 0 ;
    while (cin >> n && n){
        cnt = 0 ;
        memset(Next,0 ,sizeof(Next)) ;
        memset(fail, 0, sizeof(fail)) ;
        memset(val, 0, sizeof(val)) ;
        for (int i = 1; i <= n; i++){
            cin >> str ;
            Insert() ;
        }
        getFail() ;
        cin >> str1 ;
        cout << "Case " << ++Cas << ": " ;
        query() ;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值