Codeforces 86C Genetic engineering (AC自動機+dp)

來源:互聯網
上載者:User

標籤:io   os   ar   for   sp   c   on   html   amp   

題目大意:

要求構造一個串,使得這個串是由所給的串相串連構成,串連可以有重疊的部分。


思路分析:

首先用所給的串建立自動機,每個單詞節點記錄當前節點能夠達到的最長尾碼。

開始的時候想的是dp[i][j]表示長度為i,走到自動機的j節點的答案。

但是顯然既然是可以重複覆蓋的,那麼每一個節點的dp值都並不是最優的,因為可以從一個地方截斷去串連另外一個串。

所以正確姿勢就是dp [i] [j] [k] 表示構造到了長度為 i 的串, 現在這個串後面有k 個字元是沒有找到有效節點的,然後在自動機上走到了j。

那麼轉移的時候,就有兩種情況。

isword >= k+1。。。為什麼是k+1 因為我們現在是去找的兒子節點,已經加1了。這樣的話就是這個節點可以完全覆蓋沒有匹配到的k個,換句話說就是讓後面的k個字元找到了合法節點去匹配。那麼就轉移到dp [i+1] [j->next] [0]...

否則,如果k+1<=10 那麼就讓後面這個繼續失配,那麼久直接轉移到 dp [i+1][j->next][k+1]...


最後累加答案。


#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <utility>#include <string>#include <vector>#define inf 0x3f3f3f3fusing namespace std;const int mod = 1000000009;const char tab = 'a';const int max_next = 4;int rev[256];struct trie{    struct trie *fail;    struct trie *next[max_next];    int isword;    int index;};struct AC{    trie *que[100005],*root,ac[100005];    int head,tail;    int idx;    trie *New()    {        trie *temp=&ac[idx];        for(int i=0;i<max_next;i++)temp->next[i]=NULL;        temp->fail=NULL;        temp->isword=0;        temp->index=idx++;        return temp;    }    void init()    {        idx=0;        root=New();    }    void Insert(trie *root,char *word,int len){        trie *t=root;        for(int i=0;i<len;i++){            if(t->next[rev[word[i]]]==NULL)                t->next[rev[word[i]]]=New();            t=t->next[rev[word[i]]];        }        t->isword=len;    }    void acbuild(trie *root){        int head=0,tail=0;        que[tail++]=root;        root->fail=NULL;        while(head<tail){            trie *temp=que[head++],*p;            for(int i=0;i<max_next;i++){                 if(temp->next[i]){                    if(temp==root)temp->next[i]->fail=root;                    else {                        p=temp->fail;                        while(p!=NULL){                            if(p->next[i]){                                temp->next[i]->fail=p->next[i];                                break;                            }                            p=p->fail;                        }                        if(p==NULL)temp->next[i]->fail=root;                    }                    if(temp->next[i]->fail->isword)                        temp->next[i]->isword=max(temp->next[i]->isword,temp->next[i]->fail->isword);                    que[tail++]=temp->next[i];                 }                 else if(temp==root)temp->next[i]=root;                 else temp->next[i]=temp->fail->next[i];            }        }    }    void tra()    {        for(int i=0;i<idx;i++)        {            if(ac[i].fail!=NULL)printf("fail = %d ",ac[i].fail->index);            for(int k=0;k<max_next;k++)                printf("%d ",ac[i].next[k]->index);            puts("");        }    }}sa,sb;string cq[55];char word[55];int dp[1005][105][11];void add(int &a,int b){    a+=b;    if(a>=mod)a-=mod;}int solve(int L){    memset(dp,0,sizeof dp);    dp[0][0][0]=1;    for(int i=0;i<L;i++)    {        for(int j=0;j<sa.idx;j++)        {            for(int k=0;k<10;k++)            {                for(int d=0;d<4;d++)                {                    if(sa.ac[j].next[d]->isword>=k+1)                        add(dp[i+1][sa.ac[j].next[d]->index][0],dp[i][j][k]);                    else if(k+1<=10)                        add(dp[i+1][sa.ac[j].next[d]->index][k+1],dp[i][j][k]);                }            }        }    }    int ans=0;    for(int i=0;i<sa.idx;i++)    {        add(ans,dp[L][i][0]);    }    return ans;}int main(){    rev['A']=0;    rev['C']=1;    rev['G']=2;    rev['T']=3;    int m,L;    while(cin>>L>>m)    {        sa.init();        for(int i=1;i<=m;i++)        {            cin>>word;            sa.Insert(sa.root,word,strlen(word));        }        sa.acbuild(sa.root);        printf("%d\n",solve(L));    }    return 0;}


Codeforces 86C Genetic engineering (AC自動機+dp)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.