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

    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$($1leq nleq 50$)个病毒DNA序列,长度均不超过20。现在给出一个长度不超过1000的字符串,求至少要更换多少个字符才能使这个字符串不包含这些DNA序列。

    AC自动机上的DP。dp[c][i]:设长度为 i 且在AC自动机中最终对应于结点 c 的字符串为s,那么将题给的字符串前i个字符至少要更换dp[c][i]个字符才能变成s。

    这里的s不是某一个字符串,它表示的是一类字符串,即所有能通过AC自动机到达结点c的串。“到达结点c”不一定只能从c的父节点到达,还可以从其他结点通过fail指针到达。所以能到达c的有好多个字符串,在这些字符串中找一条最接近题给字符串的,选哪条字符串不影响后续操作。

    和大多数AC+DP题一样,最外层循环是对字符串长度的循环,和之前做的不一样,这里不是压缩dp,但思路差不多,当循环到dp[c][i]时,dp[c][i]已经计算好了,所以下面计算与c相连的结点,如果不存在相应结点,则需要用到fail指针进行跳跃。

    这也是AC+DP大家族中的一员,值得学习。

    #include<cstdio>
    #include<cstring>
    #include<queue>
    using namespace std;
    const int r=22;
    const int l=52;
    const int inf = 999999;
    int ch[r*l][4],End[r*l],cur,fail[r*l],last[r*l];
    char str[1005],str0[l][r];
    void get_fail()
    {
        int now,tmpFail,Next;
        queue<int> q;
        for(int j=0; j<4; j++)
        {
            if(ch[0][j])
            {
                q.push(ch[0][j]);
                fail[ch[0][j]] = 0;
                last[ch[0][j]] = 0;
            }
        }
        while(!q.empty())
        {
            now = q.front();
            q.pop();
            for(int j=0; j<4; j++)
            {
                if(!ch[now][j]) continue;
                Next = ch[now][j];
                q.push(Next);
                tmpFail = fail[now];
                while(tmpFail&&!ch[tmpFail][j]) tmpFail = fail[tmpFail];
                fail[Next] = ch[tmpFail][j];
                last[Next] = End[fail[Next]] ? fail[Next]:last[fail[Next]];
            }
        }
    }
    int dp[1005][1005];
    int main()
    {
        int n,now,kase=1;
        while(scanf("%d",&n)!=EOF&&n)
        {
            memset(ch,0,sizeof(ch));
            memset(End,0,sizeof(End));
            memset(last,0,sizeof(last));
            memset(dp,-1,sizeof(dp));
            cur = 1;
            int len;
            for(int i=1; i<=n; i++)
            {
                scanf("%s",str0[i]);
                len = strlen(str0[i]);
                now = 0;
                for(int j=0; j<len; j++)
                {
                    if(str0[i][j]=='A') str0[i][j]=0;
                    else if(str0[i][j]=='T') str0[i][j]=1;
                    else if(str0[i][j]=='G') str0[i][j]=2;
                    else if(str0[i][j]=='C') str0[i][j]=3;
                    if(ch[now][str0[i][j]]==0) ch[now][str0[i][j]] = cur++;
                    now = ch[now][str0[i][j]];
                }
                End[now] = i;
            }
            get_fail();
            scanf("%s",str);
    
            len = strlen(str);
    
            for(int i=0;i<len;i++) {
                if(str[i]=='A') str[i]=0;
                else if(str[i]=='T') str[i]=1;
                else if(str[i]=='G') str[i]=2;
                else if(str[i]=='C') str[i]=3;
            }
            dp[0][0]=0;
            int ans=inf;
            for(int i=0; i<len; i++)
            {
                for(int tnow=0; tnow<cur; tnow++)
                {
                    if(dp[tnow][i]!=-1&&dp[tnow][i]<inf)
                    //if(dp[tnow][i]!=-1)
                    for(int c=0; c<4; c++)
                    {
                        int now=tnow;
                        while(now&&!ch[now][c]) now = fail[now];
                        now = ch[now][c];
                        if(End[now]||last[now]) {
                            dp[now][i+1]=inf;
                            continue;
                        }
                        dp[now][i+1] = min(dp[now][i+1]==-1?inf:dp[now][i+1],dp[tnow][i] + (c==str[i]?0:1));
                        if(i==len-1) {
                            ans=min(ans,dp[now][i+1]);
                            //printf("%d
    ",dp[now][i+1]);
                        }
                    }
                }
            }
            if(ans>=inf) ans=-1;
            printf("Case %d: %d
    ",kase++,ans);
        }
    }
  • 相关阅读:
    Objective-C写出Json文件(可作配置文件)
    关于快速排序的部分内容
    关于折半查找排序的部分内容
    异步下载网络图片
    pytest(5):setup/teardown框架结构
    pytest(4):用例参数化
    pytest(3):pytest运行参数介绍
    pytest(2):使用pycharm运行pytest
    pytest(1):pytest的安装与使用
    pytest文档4-Allure报告清除上一次数据
  • 原文地址:https://www.cnblogs.com/lastone/p/5371531.html
Copyright © 2011-2022 走看看