poj 2778 AC自動機+矩陣快速冪

來源:互聯網
上載者:User

題目:DNA Sequence

這題做了一晚上,即使大體的思路明白了,但還是糾纏與各種細節的實現

#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <algorithm>using namespace std;#define mod 100000#define pb push_backstruct node {    int son[4],fail;    bool flag;    node(){memset(son,0,sizeof(son));}    node(bool f):flag(f){memset(son,0,sizeof(son));}};vector<node> ac;int hash[256];struct matrix {    long long a[101][101];    matrix(){memset(a,0,sizeof(a));}    matrix(long long p) {memset(a,0,sizeof(a));set(p);}    void set(long long p) {        for(int i=0;i<ac.size();++i)                a[i][i]=p;    }    matrix operator*(const matrix &p) {        matrix res;        for(int i=0;i<ac.size();++i)            for(int j=0;j<ac.size();++j) {                for(int k=0;k<101;++k)                    res.a[i][j]+=this->a[i][k]*p.a[k][j];                res.a[i][j]%=mod;            }        return res;    }    long long sum() {        long long res=0;        for(int i=0;i<ac.size();++i)                res+=this->a[0][i],res%=mod;        return res;    }}ans;void insert(char *str) {    int p=0,i=0;    while(str[p]) {        if(ac[i].flag) break;//危險節點後不用再加兒子        if(!ac[i].son[hash[str[p]]]) {            ac[i].son[hash[str[p]]]=ac.size();            ac.pb(node(false));        }        i=ac[i].son[hash[str[p]]];        p++;    }    ac[i].flag=true;}void getFail() {    ac[0].fail=-1;    queue<int> qu;    qu.push(0);    while(!qu.empty()) {        int cur=qu.front();qu.pop();        if(ac[cur].flag) continue;//危險節點不需要拓展子節點,或計入答案        for(int i=0;i<4;++i) {            int nex=ac[cur].son[i];            if(nex) {                qu.push(nex);                int p=ac[cur].fail;                while(~p&&!ac[p].son[i]) p=ac[p].fail;                if(~p) ac[nex].fail=ac[p].son[i],ac[nex].flag|=ac[ac[nex].fail].flag;//若fail指向的節點是危險節點,則nex包含危險節點,不用計入答案                else ac[nex].fail=0;            }            if(nex&&!ac[nex].flag) ++ans.a[cur][nex];            else if(!nex) {                int p=ac[cur].fail;                while(~p&&!ac[p].son[i]) p=ac[p].fail;//遇到第一個匹配點就應跳出,而不是碰到匹配的危險節點後繼續往上走,這相當於掩耳盜鈴                if(~p&&!ac[ac[p].son[i]].flag) ++ans.a[cur][ac[p].son[i]];//如果fail指向的節點是危險節點,就不應計入答案                else if(!~p) ++ans.a[cur][0];            }        }    }}matrix qpow(int p) {    matrix res(1);    while(p) {        if(p&1) res=res*ans;        ans=ans*ans;        p>>=1;    }    return res;}int main() {    ios::sync_with_stdio(false);    hash['A']=0;    hash['T']=1;    hash['C']=2;    hash['G']=3;    ac.pb(node(false));    int m,n;    scanf("%d%d",&m,&n);    char str[20];    while(m--) {        scanf("%s",str);        insert(str);    }    getFail();    printf("%d\n",qpow(n).sum());    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.