zoukankan      html  css  js  c++  java
  • POJ 3691 DNA repair [AC自动机 DP]

    DNA repair
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 6758   Accepted: 3133

    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

    Source


    题意:给出n个模式串,和一个长度为m的原串,求最少修改几位,使得其中不包含任何一个模式串为子串

    套路......
    f[i][j]表示到了原串的第i位,AC自动机上走到节点j不含模式串最少修改几位
    转移枚举不改和改成什么
     
    愚蠢的s[i+1]忘加mp[]......
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int N=1005,INF=1e9;
    inline int read(){
        char c=getchar();int x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
        return x*f;
    }
    int n,m,mp[N];
    char s[N];
    struct node{
        int ch[5],fail,val;
    }t[N];
    int sz;
    void ins(char s[]){
        int u=0,n=strlen(s+1);
        for(int i=1;i<=n;i++){
            int c=mp[s[i]];
            if(!t[u].ch[c]) t[u].ch[c]=++sz;
            u=t[u].ch[c];
        }
        t[u].val=1;
    }
    int q[N],head,tail;
    void getAC(){
        head=tail=1;
        for(int i=1;i<=4;i++) if(t[0].ch[i]) q[tail++]=t[0].ch[i];
        while(head!=tail){
            int u=q[head++];
            t[u].val|=t[t[u].fail].val;
            for(int i=1;i<=4;i++){
                int &v=t[u].ch[i];
                if(!v) v=t[t[u].fail].ch[i];
                else{
                    t[v].fail=t[t[u].fail].ch[i];
                    q[tail++]=v;
                }
            }
        }
    }
    int f[N][N];
    void dp(){
        memset(f,127,sizeof(f));
        f[0][0]=0;
        for(int i=0;i<m;i++)
            for(int j=0;j<=sz;j++) if(f[i][j]<INF){
                int k=t[j].ch[mp[s[i+1]]];
                if(!t[k].val) f[i+1][k]=min(f[i+1][k],f[i][j]);
                for(int k=1;k<=4;k++) if(!t[t[j].ch[k]].val)
                    f[i+1][t[j].ch[k]]=min(f[i+1][t[j].ch[k]],f[i][j]+1);
            }
        //for(int i=1;i<=m;i++)
        //    for(int j=0;j<=sz;j++) printf("hi %d %d %d
    ",i,j,f[i][j]);
        int ans=INF;
        for(int i=0;i<=sz;i++) ans=min(ans,f[m][i]);
        if(ans>=INF) puts("-1");
        else printf("%d
    ",ans);
    }
    int main(){
        freopen("in","r",stdin);
        int cas=0;
        mp['A']=1;mp['G']=2;mp['C']=3;mp['T']=4;
        while((n=read())){ printf("Case %d: ",++cas);
            memset(t,0,sizeof(t));sz=0;
            for(int i=1;i<=n;i++) scanf("%s",s+1),ins(s);
            scanf("%s",s+1);
            m=strlen(s+1);
            getAC();
            dp();
        }
    }
     
     
     
  • 相关阅读:
    kmp模板
    2017 ACM/ICPC Asia Regional Shenyang Online transaction transaction transaction
    2017 ACM/ICPC Asia Regional Shenyang Online 12 card card card
    KMP
    最长不下降子序列
    codeforces round 433 D. Jury Meeting
    codeforces round 433 C. Planning 贪心
    hdu 5792 线段树+离散化+思维
    hdu 5792 树状数组+离散化+思维
    hdu 5791 思维dp
  • 原文地址:https://www.cnblogs.com/candy99/p/6368292.html
Copyright © 2011-2022 走看看