• <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>

            hnu 2243 考研路茫茫——單詞情結(jié) AC自動機(jī)+矩陣冥累加和

               這個題目更奇葩。據(jù)說是上一個題的加強(qiáng)版。
               題意是給定M個模式串,然后給定長度L,問不超過L的文本至少含有一個模式的情況的總種數(shù)。

               還是用模式串建立Trie圖,根據(jù)Trie圖建立起路徑長度為1的矩陣M。
               總情況數(shù)目為26^1+26^2+...+26^L。不含模式串的情況總數(shù)為矩陣N = M^1+M^2+M^3
            +...+M^L的第一行之和。總情況數(shù)目減去不含模式串的情況就是答案。
               這里用到了矩陣的一些算法,比如快速冥,還有快速冥求和。但是,我用了操作符重載,最悲劇
            的是重載后的操作符沒有優(yōu)先級,而我還當(dāng)作有優(yōu)先級的在用,所以悲劇了。。。一直樣例都過不
            去。。。唉,最后才發(fā)現(xiàn)了這個問題。。。寫了260行左右的代碼,前面的一部分代碼可以當(dāng)作矩
            陣操作的模板了。。。Trie圖的也不錯,過幾天估計得打印下來用了。。。

               代碼如下:
            #include <stdio.h>
            #include <string.h>
            #include <queue>
            #include <algorithm>
            using namespace std;

            typedef unsigned long long INT;
            const int MAX_D = 26;
            const int MAX_L = 10;
            const int MAX_N = 10;
            char szPat[MAX_L];

            const int MAX_S = 31;
            struct Matrix
            {
                int nSize;
                INT nD[MAX_S][MAX_S];
                Matrix(int nS)
                {
                    Clear(nS);
                }

                Matrix& operator = (const Matrix& m)
                {
                    nSize = m.nSize;
                    for (int i = 0; i < nSize; ++i)
                    {
                        for (int j = 0; j < nSize; ++j)
                        {
                            nD[i][j] = m.nD[i][j];
                        }
                    }
                    return *this;
                }
                void Clear(int nS)
                {
                    nSize = nS;
                    memset(nD, 0, sizeof(nD));
                }
                void Unit()
                {
                    for (int i = 0; i < nSize; ++i)
                    {
                        for (int j = 0; j < nSize; ++j)
                        {
                            nD[i][j] = (i == j ? 1 : 0);
                        }
                    }
                }
            };

            Matrix operator+(const Matrix& A, const Matrix& B)
            {
                Matrix C(A.nSize);

                for (int i = 0; i < A.nSize; ++i)
                {
                    for (int j = 0; j < A.nSize; ++j)
                    {
                        C.nD[i][j] = A.nD[i][j] + B.nD[i][j];
                    }
                }
                return C;
            }

            Matrix operator*(const Matrix& nA, const Matrix& nB)
            {
                Matrix nC(nB.nSize);
                for (int i = 0; i < nA.nSize; ++i)
                {
                    for (int j = 0; j < nA.nSize; ++j)
                    {
                        for (int k = 0; k < nA.nSize; ++k)
                        {
                            nC.nD[i][j] += nA.nD[i][k] * nB.nD[k][j];
                        }
                    }
                }
                return nC;
            }

            Matrix operator^(Matrix B, INT nExp)
            {
                Matrix ans(B.nSize);

                ans.Unit();
                while (nExp)
                {
                    if (nExp % 2)
                    {
                        ans = ans * B;
                    }
                    B = B * B;
                    nExp >>= 1;
                }
                return ans;
            }

            //求base^1+base^2++base^N
            Matrix SumPowMatrix(Matrix& base, INT nN)
            {
                if (nN == 1)
                {
                    return base;
                }

                Matrix ans = SumPowMatrix(base, nN / 2);
                ans = ans + ((base^(nN / 2)) * ans);//重載運(yùn)算符保證不了優(yōu)先級
                if (nN % 2)
                {
                    ans = ans + (base^nN);//沒優(yōu)先級啊,必須加括號,查錯2個小時了
                }
                return ans;
            }

            struct Trie
            {
                Trie* next[MAX_D];
                Trie* fail;
                int no;
                bool flag;
            };
            Trie tries[MAX_L * MAX_N];
            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(Trie* pRoot, char* pszPat)
            {
                Trie* pNode = pRoot;
                while (*pszPat)
                {
                    int idx = *pszPat - 'a';
                    if (pNode->next[idx] == NULL)
                    {
                        pNode->next[idx] = NewNode();
                    }
                    pNode = pNode->next[idx];
                    ++pszPat;
                }
                pNode->flag = true;
            }

            void BuildAC(Trie* pRoot, Matrix& M)
            {
                pRoot->fail = NULL;
                queue<Trie*> qt;
                qt.push(pRoot);

                M.Clear(nP);
                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)
                            {
                                front->next[i]->flag = true;
                            }
                            qt.push(front->next[i]);
                        }
                        else
                        {
                            front->next[i] = front == pRoot? pRoot : front->fail->next[i];
                        }

                        //這里必須要加上front->flag為false的判斷么?加不加會生成不同的矩陣
                        if (!front->next[i]->flag)
                        {
                            ++M.nD[front->no][front->next[i]->no];
                        }
                    }
                }
            }

            int main()
            {
                int nN;
                INT nL;
                Matrix M(0);

                while (scanf("%d%I64u", &nN, &nL) == 2)
                {
                    InitTrie(pRoot);
                    while (nN--)
                    {
                        scanf("%s", szPat);
                        Insert(pRoot, szPat);
                    }
                    BuildAC(pRoot, M);

                    Matrix tmp(1);
                    tmp.nD[0][0] = 26;
                    tmp = SumPowMatrix(tmp, nL);
                    INT nAns = tmp.nD[0][0];
                    Matrix msum = SumPowMatrix(M, nL);
                    for (int i = 0; i < msum.nSize; ++i)
                    {
                        nAns -= msum.nD[0][i];
                    }
                    printf("%I64u\n", nAns);
                }

                return 0;
            }

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

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

            導(dǎo)航

            統(tǒng)計

            公告

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            me

            好友

            同學(xué)

            網(wǎng)友

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            日韩精品久久无码中文字幕| 欧美成a人片免费看久久| 久久无码国产专区精品| 一本大道久久东京热无码AV| 99久久综合国产精品免费| 亚洲精品乱码久久久久久中文字幕 | 精品无码久久久久久久动漫| 久久久久免费视频| 久久婷婷国产综合精品| 欧美久久精品一级c片片| 四虎影视久久久免费观看| 久久亚洲精品成人av无码网站 | 国产精品久久久久乳精品爆| 色狠狠久久综合网| 99久久精品毛片免费播放| 亚洲国产日韩欧美久久| 国产亚洲婷婷香蕉久久精品| 亚洲午夜精品久久久久久app| 久久综合狠狠综合久久| 久久久噜噜噜久久| 99久久超碰中文字幕伊人| 亚洲乱码日产精品a级毛片久久| 国内精品久久国产大陆| 无码专区久久综合久中文字幕| 久久久精品国产亚洲成人满18免费网站 | 久久综合精品国产一区二区三区 | 久久人人爽人人爽人人片av高请| 精品久久久久久国产牛牛app| 国产成年无码久久久久毛片| 一本久久免费视频| 九九久久精品国产| 99久久婷婷国产综合精品草原| 麻豆久久| 久久精品夜色噜噜亚洲A∨| 色偷偷888欧美精品久久久| 久久综合88熟人妻| 人人狠狠综合久久88成人| 久久久久久免费视频| 婷婷久久精品国产| 久久99九九国产免费看小说| 伊人久久精品影院|