zoukankan      html  css  js  c++  java
  • HDU 2457/POJ 3691 DNA repair AC自动机+DP

    DNA repair

    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
     
    题意:
      
      给你n个模式串,给出一个原串,问最少需要修改多少个字符,使得原串中不包含任何一个模式串
      
    题解:
     
      将这个n个模式串建立AC自动机
      设定dp[i][j]表示在trie树上第j个节点的状态下能让前i个字符保持不包含任何一个模式串的最小修改量
      简单转移
     
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    #pragma comment(linker, "/STACK:102400000,102400000")
    #define ls i<<1
    #define rs ls | 1
    #define mid ((ll+rr)>>1)
    #define pii pair<int,int>
    #define MP make_pair
    typedef long long LL;
    const long long INF = 1e18+1LL;
    const double Pi = acos(-1.0);
    const int N = 5e5+10, M = 1e3+20, mod = 1e9+7, inf = 1e9;
    
    int dp[1100][2100],sum[N],nex[N][5],cnt,head,tail,q[N],fail[N];
    
    int ID(char x) {
        if(x == 'A') return 1;
        else if(x == 'T') return 2;
        else if(x == 'C') return 3;
        else return 0;
    }
    void insert(char *s) {
        int now = 1,len = strlen(s);
        for(int i = 0; i < len; ++i) {
            int index = ID(s[i]);
            if(!nex[now][index])
                nex[now][index] = ++cnt;
            //sum[nex[now][index]] |= sum[now];
            now = nex[now][index];
        }
        sum[now] = 1;
    }
    
    void build_fail() {
        head = 0, tail = 0;
        for(int i = 0; i < 4; ++i) nex[0][i] = 1;
        fail[1] = 0;
        q[tail++] = 1;
        while(head!=tail) {
            int now = q[head++];
            sum[now] |= sum[fail[now]];
            for(int i = 0; i < 4; ++i) {
                int p = fail[now];
                if(!nex[now][i]) {
                    nex[now][i] = nex[p][i];continue;
                }
                fail[nex[now][i]] = nex[p][i];
                q[tail++] = nex[now][i];
            }
        }
    }
    int n;
    char s[N],a[N];
    int main() {
        int cas = 1;
        while(scanf("%d",&n)!=EOF) {
            if(n == 0) break;
            cnt = 1;
            memset(sum,0,sizeof(sum));
            memset(nex,0,sizeof(nex));
            memset(fail,0,sizeof(fail));
            for(int i = 1; i <= n; ++i) {
                scanf("%s",s);
                insert(s);
            }
            build_fail();
            scanf("%s",a+1);
            n = strlen(a+1);
            for(int i = 0; i <= n; ++i)
                for(int j = 1; j <= cnt; ++j)
                    dp[i][j] = inf;
            dp[0][1] = 0;
            for(int i = 0; i < n; ++i) {
                for(int j = 1; j <= cnt; ++j) {
                    if(dp[i][j] == inf) continue;
                    for(int k = 0; k < 4; ++k) {
                        if(k == ID(a[i+1])) {
                            if(!sum[nex[j][k]])dp[i+1][nex[j][k]] = min(dp[i+1][nex[j][k]],dp[i][j]);
                        }
                        else {
                           if(!sum[nex[j][k]]) dp[i+1][nex[j][k]] = min(dp[i+1][nex[j][k]],dp[i][j]+1);
                        }
                    }
                }
            }
            int ans = inf;
            for(int i = 1; i <= cnt; ++i) {
                ans = min(dp[n][i],ans);
            }
            printf("Case %d: ",cas++);
            if(ans == inf) puts("-1");
            else printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    freemarker 遍历 hashmap 到select option
    三分钟跑起jsblocks
    hibernate_@GeneratedValue
    跑github上的Symfony项目遇到的问题2
    Activiti使用过程_1
    Symfony官方视频教程
    跑github上的Symfony项目遇到的问题
    剧本杀
    随遇而安
    开发者职场心得
  • 原文地址:https://www.cnblogs.com/zxhl/p/6596410.html
Copyright © 2011-2022 走看看