• poj 3691 DNA repair


    DNA repair
    Time Limit: 2000MS   Memory Limit: 65536K
         

    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的原串,求最少修改几位,使得其中不包含任何一个模式串为子串

    字母只有AGCT

    dp[i][j]表示已经修改好了前i位u,当前在AC自动机的j节点,且前i位不包含任何一个模式串位子串的最小修改次数

    不改,dp[i][j]=dp[i+1][k]

    改 ,dp[i][j]=dp[i+1][k]+1

    取min

    如果dp[i+1][k]是单词节点就不能转移

    记忆化搜索即可

    #include<queue>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int n,len,root,id,ans;
    char s[1001];
    bool mark[51*21],v[1001][51*21];
    int trie[51*21][5],tot,f[51*21],dp[1001][51*21];
    queue<int>q;
    struct ACautomata
    {
        int get(char c)
        {
            if(c=='A') return 0;
            if(c=='G') return 1;
            if(c=='C') return 2;
            if(c=='T') return 3;
        }
        void insert()
        {
            root=1;
            len=strlen(s);
            for(int i=0;i<len;i++)
            {
                id=get(s[i]);
                if(!trie[root][id]) 
                {
                    trie[root][id]=++tot;
                    memset(trie[tot],0,sizeof(trie[tot]));
                    mark[tot]=0;
                }
                root=trie[root][id];
            }
            mark[root]=true;
        }
        void getfail()
        {
            memset(f,0,sizeof(f));
            q.push(1);
            int now,j;
            while(!q.empty())
            {
                now=q.front(); q.pop();
                for(int i=0;i<4;i++)
                {
                    if(!trie[now][i])
                    {
                        trie[now][i]=trie[f[now]][i];
                        continue;
                    }
                    q.push(trie[now][i]);
                    j=f[now];
                    f[trie[now][i]]=trie[j][i];
                    if(mark[trie[j][i]]) mark[trie[now][i]]=true;
                }
            }
        }
        int dfs(int l,int now)
        {
            if(l==len) return 0;
            if(v[l][now]) return dp[l][now];
            v[l][now]=true;
            if(!mark[trie[now][get(s[l+1])]]) 
              dp[l][now]=dfs(l+1,trie[now][get(s[l+1])]);
            else dp[l][now]=2000;
            for(int i=0;i<4;i++)
             if(!mark[trie[now][i]]&&i!=get(s[l+1]))  
               dp[l][now]=min(dp[l][now],dfs(l+1,trie[now][i])+1);
            return dp[l][now];
        }
    };
    ACautomata AC;
    int main()
    {
        for(int i=0;i<4;i++) trie[0][i]=1;
        int t=0;
        while(scanf("%d",&n)!=EOF)
        {
            if(!n) return 0;
            tot=1;
            memset(v,0,sizeof(v));
            memset(trie[1],0,sizeof(trie[1]));
            memset(dp,0,sizeof(dp));
            while(n--)
            {
                scanf("%s",s);
                AC.insert();
            }
            AC.getfail();
            scanf("%s",s+1);
            len=strlen(s+1);
            ans=AC.dfs(0,1);
            if(ans==2000) ans=-1;
            printf("Case %d: %d
    ",++t,ans);
        }
    }
  • 相关阅读:
    redis集群redis-cloud搭建
    Linux下搭建redis服务器
    mybatis主键返回
    分布式文件系统FastDFS
    mapper映射文件不发布
    dubbo发布和引用服务
    PageHelper分页插件
    F. Cards and Joy
    E. Paint the Tree 树形dp
    D. Sequence Sorting dp
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/6970800.html
Copyright © 2020-2023  润新知