POJ 2778 DNA Sequence (ac automata + Matrix fast Power)

Source: Internet
Author: User

Topic Link: DNA Sequence



Analysis: AC automata + matrix acceleration (fast power).

The idea of a state transfer diagram for an AC automaton at this time is very thorough, and the AC automata is the ability to determine the transfer of the state.



AC Code:

#include <iostream> #include <cstdio> #include <queue> #include <cstring>using namespace std;    const int MOD = 100000;struct matrix{int mat[110][110], n;        Matrix () {} matrix (int _n) {n = _n;    for (int i = 0, i < n; i++) for (int j = 0; J < N; j + +) Mat[i][j] = 0;        } Matrix operator * (const matrix &AMP;B) const{MATRIX ret = Matrix (n);                    for (int i = 0, i < n; i++) for (int j = 0; J < N; j + +) for (int k = 0; k < n; k++) {                    int tmp = (long Long) mat[i][k] * B.mat[k][j]% MOD;                RET.MAT[I][J] = (Ret.mat[i][j] + tmp)% MOD;    } return ret;    }};struct trie{int next[110][4], fail[110];    BOOL end[110];    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; for (int i = 0; i < 4; i + +) {if (next[now][i] = = 1) next[now][i] = next[Fail[now] [                I]; else{fail[Next[noW][i]] = next[Fail[now]][i];                Q.push (Next[now][i]);        }}}} Matrix Getmatrix () {Matrix ret = matrix (L);                    for (int i = 0; i < L; i + +) for (int j = 0; J < 4; j + +) if (end[next[i][j] = = False)        ret.mat[i][Next[i][j]] + +;    return ret; }}; Trie Ac;char buf[20];    Matrix Pow_mat (Matrix A, int n) {//fast power MATRIX ret = matrix (A.N);    for (int i = 0; i < RET.N; i + +) ret.mat[i][i] = 1;    Matrix tmp = A;        while (n) {if (N & 1) ret = RET * TMP;        TMP = TMP * TMP;    n >>= 1; } return ret;}    int main () {#ifdef sxk freopen ("In.txt", "R", stdin);    #endif//sxk int n, m;        while (scanf ("%d%d", &m, &n) = = 2) {ac.init ();            for (int i = 0; i < m; i + +) {scanf ("%s", buf);        Ac.insert (BUF);        } ac.build ();        Matrix a = Ac.getmatrix ();      A = Pow_mat (A, n);  int ans = 0;        for (int i = 0; i < A.N; i + +) ans = (ans + a.mat[0][i])% MOD;    printf ("%d\n", ans); } return 0;}





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

POJ 2778 DNA Sequence (ac automata + Matrix fast Power)

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.