zoukankan      html  css  js  c++  java
  • HDU 2457 DNA repair

    DNA repair

    Time Limit: 2000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 2457
    64-bit integer IO format: %I64d      Java class name: Main
    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

     
    解题:AC自动机 + dp 其实是Trie图
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int INF = 0x3f3f3f3f;
     4 const int maxn = 1010;
     5 char str[maxn];
     6 int dp[maxn][maxn],id[130],n,cs = 1;
     7 
     8 struct Trie {
     9     int ch[maxn][4],fail[maxn],tot;
    10     bool flag[maxn];
    11     int newnode() {
    12         memset(ch[tot],0,sizeof ch[tot]);
    13         fail[tot] = flag[tot] = 0;
    14         return tot++;
    15     }
    16     void init() {
    17         tot = 0;
    18         newnode();
    19     }
    20     void insert(char *str,int root = 0){
    21         for(int i = 0; str[i]; ++i){
    22             if(!ch[root][id[str[i]]]) ch[root][id[str[i]]] = newnode();
    23             root = ch[root][id[str[i]]];
    24         }
    25         flag[root] = true;
    26     }
    27     void build(int root = 0) {
    28         queue<int>q;
    29         for(int i = 0; i < 4; ++i) {
    30             if(ch[root][i]) {
    31                 fail[ch[root][i]] = root;
    32                 q.push(ch[root][i]);
    33             }
    34         }
    35         while(!q.empty()) {
    36             root = q.front();
    37             q.pop();
    38             for(int i = 0; i < 4; ++i) {
    39                 int &nx = ch[root][i];
    40                 if(nx) {
    41                     fail[nx] = ch[fail[root]][i];
    42                     flag[nx] = flag[nx]||flag[fail[nx]];
    43                     q.push(nx);
    44                 } else nx = ch[fail[root]][i];
    45             }
    46         }
    47     }
    48     void solve(char *str) {
    49         memset(dp,0x3f,sizeof dp);
    50         int len = strlen(str);
    51         dp[0][0] = 0;
    52         for(int i = 0; i < len; ++i)
    53             for(int j = 0; j < tot; ++j) {
    54                 if(dp[i][j] >= INF) continue;
    55                 for(int k = 0; k < 4; ++k) {
    56                     int nx = ch[j][k];
    57                     if(flag[nx]) continue;
    58                     dp[i+1][nx] = min(dp[i+1][nx],dp[i][j] + (id[str[i]] != k));
    59                 }
    60             }
    61         int ret = INF;
    62         for(int i = 0; i < tot; ++i)
    63             ret = min(ret,dp[len][i]);
    64         printf("Case %d: %d
    ",cs++,ret == INF?-1:ret);
    65     }
    66 } ac;
    67 int main() {
    68     for(int i = 0; i < 4; ++i) id["ATCG"[i]] = i;
    69     while(scanf("%d",&n),n) {
    70         ac.init();
    71         for(int i = 0; i < n; ++i) {
    72             scanf("%s",str);
    73             ac.insert(str);
    74         }
    75         ac.build();
    76         scanf("%s",str);
    77         ac.solve(str);
    78     }
    79     return 0;
    80 }
    View Code
     
  • 相关阅读:
    UART中的硬件流控RTS与CTS
    Yii smsto短信接口的函数
    分布式选主 利用Mysql ACID和Lease协议实现选主和高可用
    Extjs GridPanel 合计功能 解决滚动条滚动问题和页面刷新滚动条回到初始位置问题。
    基于ARM+LINUX的无线视频采集系统设计搭建嵌入式web服务器
    C++里父类的析构函数为什么声明为virtual
    重量过载 = 重载
    Spread Studio中文支持图解
    Android用户如何FQ访问被封锁的媒体资源
    将myeclipse 10.x以下版本web project的导入到myeclipse blue 2013 部署没有项目名
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4934694.html
Copyright © 2011-2022 走看看