• hdu 2457 AC自动机+dp


    DNA repair

    Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2004    Accepted Submission(s): 1085


    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
    /*
    hdu 2457 AC自动机+dp
    
    给你n个子串和一个主串. 对主串最少修改多少次后使其不包含子串
    通过AC自动机能够处理出 一个状态转移图
    用dp[i][j]表示长度为i,当前位置为j的最小修改数. nex[j][k] 状态k的节点编号
    如果当前位置与主串相同则不需要修改, 否则 +1
    而且通过ed数组我们判断当前是否已经走到任意子串的结尾.
    那么 dp[i][nex[j][k]] = min(dp[i][nex[j][k]],dp[i-1][j] + (k == str[i]? 0:i));
    
    hhh-2016-04-24 11:23:59
    */
    #include <iostream>
    #include <vector>
    #include <cstring>
    #include <string>
    #include <cstdio>
    #include <queue>
    #include <algorithm>
    #include <functional>
    #include <map>
    using namespace std;
    #define lson  (i<<1)
    #define rson  ((i<<1)|1)
    typedef unsigned long long ll;
    typedef unsigned int ul;
    const int INF = 0x3f3f3f3f;
    int tot;
    int dp[2][1010];
    
    //struct Matrix
    //{
    //    int len;
    //    int ma[1010][1010];
    //    Matrix() {}
    //    Matrix(int L)
    //    {
    //        len = L;
    //    }
    //};
    
    struct Tire
    {
        int nex[1010][4],fail[1010],ed[1010];
        int root,L;
        int newnode()
        {
            for(int i = 0; i < 4; i++)
                nex[L][i] = -1;
            ed[L++] = 0;
            return L-1;
        }
    
        void ini()
        {
            L = 0,root = newnode();
        }
    
        int cal(char ch)
        {
            if(ch == 'A')
                return 0;
            else if(ch == 'C')
                return 1;
            else if(ch == 'G')
                return 2;
            else if(ch == 'T')
                return 3;
        }
    
        void inser(char buf[])
        {
            int len = strlen(buf);
            int now = root;
            for(int i = 0; i < len; i++)
            {
                int ta = cal(buf[i]);
                if(nex[now][ta] == -1)
                    nex[now][ta] = newnode();
                now = nex[now][ta];
            }
            ed[now]  ++;
        }
    
        void build()
        {
            queue<int >q;
            fail[root] = root;
            for(int i = 0; i < 4; i++)
                if(nex[root][i] == -1)
                    nex[root][i] = root;
                else
                {
                    fail[nex[root][i]] = root;
                    q.push(nex[root][i]);
                }
            while(!q.empty())
            {
                int now = q.front();
                q.pop();
                if(ed[fail[now]])
                    ed[now] = 1;
                for(int i = 0; i < 4; i++)
                {
                    if(nex[now][i] == -1)
                        nex[now][i] = nex[fail[now]][i];
                    else
                    {
                        fail[nex[now][i]] = nex[fail[now]][i];
                        q.push(nex[now][i]);
                    }
                }
            }
        }
    
    //    Matrix to_mat()
    //    {
    //        Matrix mat(L);
    //        memset(mat.ma,0,sizeof(mat.ma));
    //        for(int i = 0; i < L; i++)
    //        {
    //            for(int j = 0; j < 4; j++)
    //            {
    //                if(!ed[nex[i][j]])
    //                    mat.ma[i][nex[i][j]] ++;
    //            }
    //        }
    //        return mat;
    //    }
    };
    
    
    
    //Matrix mat;
    Tire ac;
    char str[1100];
    char buf[22];
    int main()
    {
        int n;
        int cas = 1;
        while(scanf("%d",&n) != EOF && n)
        {
            ac.ini();
            for(int i = 1; i <= n; i++)
            {
                scanf("%s",buf);
                ac.inser(buf);
            }
            ac.build();
    //        mat = ac.to_mat();
            for(int i = 0; i < ac.L; i++)
                dp[0][i] = INF;
            dp[0][0] = 0;
            int cur = 0;
            scanf("%s",str);
            for(int i = 0; i < (int)strlen(str); i++)
            {
                cur ^= 1;
                for(int i = 0; i < ac.L; i++)
                    dp[cur][i] = INF;
                for(int j = 0; j < ac.L; j++)
                {
                    for(int k = 0; k < 4; k++)
                    {
                        if(dp[cur^1][j] != INF && !ac.ed[ac.nex[j][k]])
                            dp[cur][ac.nex[j][k]] = min(dp[cur][ac.nex[j][k]] , dp[cur^1][j] + (k == ac.cal(str[i]) ? 0:1));
                    }
                }
    
    
            }
            int ans = INF;
            for(int i = 0; i < ac.L; i++)
                    ans = min(ans,dp[cur][i]);
            printf("Case %d: ",cas++);
            if(ans == INF)
                cout << -1 <<"
    ";
            else
                cout << ans <<"
    ";
        }
        return 0;
    }
    

      

  • 相关阅读:
    centos 6.5 下安装RabbitMQ-3.7.28 二进制版本
    Centos 6.5 Rabbitmq 安装和集群,镜像部署
    Vim 自动添加脚本头部信息
    vim 手动添加脚本头部信息
    Pandas系列教程(11)Pandas的索引index
    Pandas系列教程(10)Pandas的axis参数
    Pandas系列教程(9)Pandas字符串处理
    Pandas系列教程(8)pandas数据排序
    Pandas系列教程(7)Pandas的SettingWithCopyWarning
    Pandas系列教程(6)Pandas缺失值处理
  • 原文地址:https://www.cnblogs.com/Przz/p/5449309.html
Copyright © 2020-2023  润新知