• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            poj 2778 DNA Sequence AC自動機+矩陣快速冥

               題意很簡單,假定文本集就是A,C,T,G,給定M個模式串,問你長度為N的文本不出現這些模式
            串的可能性到底有多少種。。。
               確實非常不直觀的樣子。。。
               解法是先學學AC自動機,建立起Trie圖,根據trie圖可以得到長度為1的路徑矩陣,然后再快速
            冥得到長度為N的路徑矩陣。
               說起來都非常糾結,沒學過AC自動機更加無法理解。學AC自動機之前據說得先學Trie樹和KMP
            才好理解。學AC自動機搞Trie圖就花費了近2天了,然后弄懂這個題又是一天,好在基本明白了。
               馬上快比賽了,從長春換到金華也不知道是好是壞。。。還是弱菜啊。。。
               貼下我的Trie圖+快速冥(直接二分了,沒有寫成數論里面那種算法)...

            #include <stdio.h>
            #include <string.h>
            #include <algorithm>
            #include <queue>
            using namespace std;

            typedef long long INT;
            const int MOD = 100000;
            const int MAX_P = 100;
            const int MAX_D = 4;
            int nIdx[256];
            char szPat[MAX_P];
            INT nMatrix[MAX_P][MAX_P];
            INT B[MAX_P][MAX_P];
            INT A[MAX_P][MAX_P];

            void InitIdx()
            {
                nIdx['A'] = 0;
                nIdx['C'] = 1;
                nIdx['T'] = 2;
                nIdx['G'] = 3;
            }

            struct Trie
            {
                Trie* fail;
                Trie* next[MAX_D];
                int no;
                bool flag;
                Trie()
                {
                    fail = NULL;
                    memset(next, 0, sizeof(next));
                    no = 0;
                    flag = false;
                }
            };
            Trie tries[MAX_D * MAX_P];
            int nP;
            Trie* pRoot;

            Trie* NewNode()
            {
                memset(&tries[nP], 0, sizeof(Trie));
                tries[nP].no = nP;
                return &tries[nP++];
            }

            void InitTrie(Trie*& pRoot)
            {
                nP = 0;
                pRoot = NewNode();
            }

            void Insert(char* pszPat)
            {
                Trie* pNode = pRoot;
                
                while (*pszPat)
                {
                    if (pNode->next[nIdx[*pszPat]] == NULL)
                    {
                        pNode->next[nIdx[*pszPat]] = NewNode();
                    }
                    pNode = pNode->next[nIdx[*pszPat]];
                    ++pszPat;
                }
                pNode->flag = true;
            }

            int BuildAC(Trie* pRoot)
            {
                memset(nMatrix, 0, sizeof(nMatrix));
                
                pRoot->fail = NULL;
                queue<Trie*> qt;
                qt.push(pRoot);
                while (!qt.empty())
                {
                    Trie* front = qt.front();
                    qt.pop();
                    
                    for (int i = 0; i < MAX_D; ++i)
                    {
                        if (front->next[i])
                        {
                            Trie* pNode = front->fail;
                            while (pNode && pNode->next[i] == NULL)
                            {
                                pNode = pNode->fail;
                            }
                            front->next[i]->fail = pNode? pNode->next[i] : pRoot;
                            if (front->next[i]->fail->flag == true)
                            {
                                front->next[i]->flag = true;
                            }
                            
                            qt.push(front->next[i]);
                        }
                        else
                        {
                            front->next[i] = front == pRoot? pRoot : front->fail->next[i];
                        }
                        
                        if (front->next[i]->flag == false)
                        {
                            nMatrix[front->no][front->next[i]->no]++;
                        }
                    }
                }
                
                return nP;//節點總個數
            }

            void MultyMatrix(INT A[][MAX_P], INT B[][MAX_P], INT C[][MAX_P], int nSize)
            {
                for (int i = 0; i < nSize; ++i)
                {
                    for (int j = 0; j < nSize; ++j)
                    {
                        INT nSum = 0;
                        for (int k = 0; k < nSize; ++k)
                        {
                            nSum = (nSum + A[i][k] * B[k][j]) % MOD;
                        }
                        C[i][j] = nSum;
                    }
                }
            }

            void CopyMatrix(INT A[][MAX_P], INT B[][MAX_P], int nSize)
            {
                for (int i = 0; i < nSize; ++i)
                {
                    for (int j = 0; j < nSize; ++j)
                    {
                        A[i][j] = B[i][j];
                    }
                }
            }

            void MatrixPower(INT M[][MAX_P], int nSize, INT nP)
            {
                if (nP == 1)
                {
                    CopyMatrix(A, M, nSize);
                    return;
                }
                
                MatrixPower(M, nSize, nP / 2);
                MultyMatrix(A, A, B, nSize);
                if (nP % 2)
                {
                    MultyMatrix(B, M, A, nSize);
                }
                else
                {
                    CopyMatrix(A, B, nSize);
                }
            }

            int main()
            {
                INT nM, nN;
                
                InitIdx();
                while (scanf("%I64d%I64d", &nM, &nN) == 2)
                {
                    InitTrie(pRoot);
                    while (nM--)
                    {
                        scanf("%s", szPat);
                        Insert(szPat);
                    }
                    int nSize = BuildAC(pRoot);
                    
                    MatrixPower(nMatrix, nSize, nN);
                    INT nAns = 0;
                    for (int i = 0; i < nSize; ++i)
                    {
                        nAns = (nAns + A[0][i]) % MOD;
                    }
                    printf("%I64d\n", nAns % MOD);
                }
                
                return 0;
            }
               
               

            posted on 2012-10-18 09:46 yx 閱讀(1230) 評論(0)  編輯 收藏 引用 所屬分類: 字符串

            <2012年10月>
            30123456
            78910111213
            14151617181920
            21222324252627
            28293031123
            45678910

            導航

            統計

            公告

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            me

            好友

            同學

            網友

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            精品综合久久久久久888蜜芽| 99国产欧美久久久精品蜜芽| 久久天天婷婷五月俺也去| 国产精品久久久久a影院| 人妻精品久久无码区| 亚洲国产精久久久久久久| 久久久久这里只有精品| 日日噜噜夜夜狠狠久久丁香五月 | 欧美成人免费观看久久| www性久久久com| 久久天天躁夜夜躁狠狠躁2022 | 麻豆久久久9性大片| 91麻精品国产91久久久久| 天天爽天天狠久久久综合麻豆| 91麻精品国产91久久久久| 色欲久久久天天天综合网| 国产亚洲色婷婷久久99精品91| 久久天天躁狠狠躁夜夜网站| 香港aa三级久久三级老师2021国产三级精品三级在 | 亚洲va久久久噜噜噜久久男同| 国产精品gz久久久| 九九99精品久久久久久| 漂亮人妻被黑人久久精品| 久久国产亚洲精品| 久久久久久午夜精品| 中文字幕精品无码久久久久久3D日动漫 | 久久99精品久久久久久水蜜桃| 久久精品国产久精国产思思| 伊人久久大香线蕉av不卡| 蜜桃麻豆WWW久久囤产精品| 久久久久亚洲av成人无码电影| 狠狠色丁香婷婷综合久久来来去| 久久精品国产影库免费看| 国内精品久久人妻互换| 国产美女久久久| 97久久天天综合色天天综合色hd | 欧美黑人又粗又大久久久| 久久夜色精品国产噜噜亚洲AV| 无码伊人66久久大杳蕉网站谷歌| 一本久久a久久精品亚洲| 久久久久无码精品国产不卡|