HDU 2457 DNA Repair (ac automaton + DP)

Source: Internet
Author: User

Topic Link: DNA Repair


Analysis: Give a sequence of n pathogenic DNA, give a fragment of DNA, ask how many bases can be modified at least to repair all pathogenic sequences in this DNA sequence.

AC Automaton + DP.

n A sequence of pathogenic DNA to form an automaton.

Make Dp[i][j] indicates that the length of I goes to Node J is the minimum number of changes required.

When the state is transferred, the next all possible bases are enumerated, and then the base is determined to reach the matching state, and if so, the safe transfer continues to enumerate the next base; otherwise, if the base is not matched, see if it is the same as the previous state, if it is different, it needs to be repaired, that is, counting plus one. If the same, direct transfer can be. Then select the minimum number of fixes.



AC Code:

#include <iostream> #include <cstdio> #include <queue> #include <cstring>using namespace std;    const int INF = 0x3f3f3f3f;struct trie{int next[1010][4], fail[1010];    BOOL end[1010];    int root, L;        int NewNode () {for (int i = 0; i < 4; i++) next[l][i] = 1;        end[l++] = false;    return L-1;        } void Init () {L = 0;    root = NewNode ();        } int getch (char ch) {if (ch = = ' A ') return 0;        if (ch = = ' C ') return 1;        if (ch = = ' G ') return 2;    else return 3;        } void Insert (char s[]) {int len = strlen (s);        int now = root; for (int i = 0; i < len; i++) {if (Next[now][getch (s[i])] = = 1) next[now][getch (s[i]) = Newno            De ();        now = Next[now][getch (S[i]);    } End[now] = true;        } void Build () {queue<int> Q;            for (int i = 0; i < 4; i + +) {if (next[root][i] = = 1) next[root][i] = root;      else{          fail[Next[root][i]] = root;            Q.push (Next[root][i]); }} while (!            Q.empty ()) {int now = Q.front ();            Q.pop ();    if (end[fail[now]] = = true) End[now] = true; Note for (int i = 0; i < 4; i + +) {if (next[now][i] = = 1) next[now][i] = NE                xt[Fail[now]][i];                    else{fail[next[now][i]] = next[Fail[now]][i];                Q.push (Next[now][i]);    }}}} int dp[1010][1010];        int solve (char buf[]) {int len = strlen (BUF);        for (int i=0, i<=len; i++) for (int j=0; j<l; j + +) dp[i][j] = INF;        Dp[0][root] = 0;                    for (int i=0, i<len; i++) for (int j=0; j<l; J + +) if (Dp[i][j] < INF) {                        for (int k=0; k<4; k++) {int newj = next[j][k]; if (END[NEWJ]) contInue;                        can safely transfer int tmp;    if (k = = Getch (buf[i])) TMP = Dp[i][j];    Same, no need to repair else TMP = dp[i][j] + 1;                    Different, number of fixes plus one dp[i+1][newj] = min (DP[I+1][NEWJ], TMP);        }} int ans = INF;    for (int i=0; i<l; i++) ans = min (ans, dp[len][i]);        Select the minimum number of fixes if (ans = = INF) ans =-1;    return ans; }};    Trie Ac;char Buf[1010];int Main () {#ifdef sxk freopen ("In.txt", "R", stdin);    #endif//sxk int n;    int kase = 0;        while (scanf ("%d", &n) = = 1 && N) {ac.init ();            for (int i = 0; i < n; i + +) {scanf ("%s", buf);        Ac.insert (BUF);        } ac.build ();        scanf ("%s", buf);    printf ("Case%d:%d\n", ++kase, Ac.solve (BUF)); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 2457 DNA Repair (ac automaton + DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.