• <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 3691 DNA repair AC自動機 + dp

               題意是給定一系列模式串。然后給出一個文本串,問至少改變文本串里面多少個字符
            可以使文本串不包含任何一個模式串。
               還是先建立Trie圖,然后在Trie圖上面進行dp。dp的思路也不是很復雜。dp[i][j]的意思
            是長度為i的文本串需要改變dp[i][j]個字符順利到達狀態j。需要注意的是長度為i的時候,
            對應的字符串中的第i-1個字符。剛開始一直沒發現這個bug。而且注意中途不能轉移到
            匹配成功的狀態上去,多加幾個條件控制即可了。。。
               轉移方程,dp[i][j] = min(dp[i][j], dp[i-1][nNext] + szText[i-1] != k),其中nNext
            是從狀態j可以轉移到的非匹配成功的狀態,k代表的當前邊的權。
               
               代碼如下:
            #include <stdio.h>
            #include <string.h>
            #include <queue>
            #include <algorithm>
            using namespace std;

            const int MAX_N = 61;
            const int MAX_L = 31;
            const int MAX_D = 4;
            const int INF = 1110;
            char chHash[256];
            char szPat[MAX_L];

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

            struct Trie
            {
                Trie* fail;
                Trie* next[MAX_D];
                bool flag;
                int no;
            };
            int nP;
            Trie* pRoot;
            Trie tries[MAX_N * MAX_L];

            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(Trie* pRoot, char* pszPat)
            {
                Trie* pNode = pRoot;
                while (*pszPat)
                {
                    int idx = chHash[*pszPat];
                    if (pNode->next[idx] == NULL)
                    {
                        pNode->next[idx] = NewNode();
                    }
                    pNode = pNode->next[idx];
                    ++pszPat;
                }
                pNode->flag = true;
            }

            void BuildAC(Trie* pRoot)
            {
                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;
                            front->next[i]->flag |= front->next[i]->fail->flag;
                            qt.push(front->next[i]);
                        }
                        else
                        {
                            front->next[i] = front == pRoot? pRoot : front->fail->next[i];
                        }
                    }
                }
            }

            int nChange[INF][INF];
            char szText[INF];

            int Solve()
            {
                int nLen = strlen(szText);
                for (int i = 0; i <= nLen; ++i)
                {
                    for (int j = 0; j < nP; ++j)
                    {
                        nChange[i][j] = INF;
                    }
                }

                int i, j, k;
                nChange[0][0] = 0;
                for (i = 1; i <= nLen; ++i)
                {
                    for (j = 0; j < nP; ++j)
                    {
                        if (tries[j].flag) continue;
                        if (nChange[i - 1][j] == INF) continue;
                        for (k = 0; k < MAX_D; ++k)
                        {
                            int nNext = tries[j].next[k] - tries;
                            if (tries[nNext].flag) continue;
                            //trie是邊權樹,所以i是從1到len,而且當前字符是szText[i-1]
                            int nTemp = nChange[i - 1][j] + (k != chHash[szText[i - 1]]);
                            nChange[i][nNext] = min(nChange[i][nNext], nTemp);
                        }
                    }
                }

                int nAns = INF;
                for (i = 0; i < nP; ++i)
                {
                    if (!tries[i].flag)
                    nAns = min(nAns, nChange[nLen][i]);
                }
                return nAns == INF? -1 : nAns;
            }

            int main()
            {
                int nN;
                int nCase = 1;

                InitHash();
                while (scanf("%d", &nN), nN)
                {
                    InitTrie(pRoot);
                    while (nN--)
                    {
                        scanf("%s", szPat);
                        Insert(pRoot, szPat);
                    }
                    BuildAC(pRoot);
                    scanf("%s", szText);
                    printf("Case %d: %d\n", nCase++, Solve());
                }

                return 0;
            }

            posted on 2012-10-21 16:53 yx 閱讀(569) 評論(0)  編輯 收藏 引用 所屬分類: 字符串

            <2012年7月>
            24252627282930
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            導航

            統計

            公告

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            me

            好友

            同學

            網友

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久久91人妻无码精品蜜桃HD| 青青草国产精品久久| 久久国产成人精品国产成人亚洲| 久久亚洲色一区二区三区| 亚洲中文字幕无码久久2017| 高清免费久久午夜精品| 久久伊人色| 久久国产亚洲高清观看| 国内精品久久久久影院老司| 国产精品久久久久久吹潮| 亚洲欧美一级久久精品| 91麻豆精品国产91久久久久久| 久久香综合精品久久伊人| 亚洲香蕉网久久综合影视| 中文字幕久久欲求不满| 一本一本久久A久久综合精品| 色成年激情久久综合| 久久亚洲中文字幕精品有坂深雪| 狠狠人妻久久久久久综合蜜桃| 久久久久亚洲精品天堂| 久久这里的只有是精品23| 综合久久一区二区三区 | 久久久久久久久波多野高潮| 久久天天日天天操综合伊人av| 久久精品人人槡人妻人人玩AV| 怡红院日本一道日本久久 | 狠狠色丁香婷综合久久| 99麻豆久久久国产精品免费| 中文成人久久久久影院免费观看| 国产成人精品久久亚洲| 亚洲美日韩Av中文字幕无码久久久妻妇 | 久久久久久久精品妇女99| 亚洲第一永久AV网站久久精品男人的天堂AV | 久久久久夜夜夜精品国产| 久久久久AV综合网成人| 人妻精品久久无码专区精东影业| 日日狠狠久久偷偷色综合0| 人妻中文久久久久| 久久这里的只有是精品23| 香蕉久久夜色精品国产2020| 亚洲人成电影网站久久|