??xml version="1.0" encoding="utf-8" standalone="yes"?>手机看片久久高清国产日韩 ,国产精品久久久久久福利漫画,久久精品这里热有精品http://m.shnenglu.com/csu-yx-2013/Algorithm Study And So Onzh-cnSat, 28 Jun 2025 04:33:41 GMTSat, 28 Jun 2025 04:33:41 GMT60poj 3264 Balanced Lineup St法建立Rmqhttp://m.shnenglu.com/csu-yx-2013/archive/2012/10/25/193854.htmlyxyxThu, 25 Oct 2012 11:29:00 GMThttp://m.shnenglu.com/csu-yx-2013/archive/2012/10/25/193854.htmlhttp://m.shnenglu.com/csu-yx-2013/comments/193854.htmlhttp://m.shnenglu.com/csu-yx-2013/archive/2012/10/25/193854.html#Feedback0http://m.shnenglu.com/csu-yx-2013/comments/commentRss/193854.htmlhttp://m.shnenglu.com/csu-yx-2013/services/trackbacks/193854.html   
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;

const int MAX_I = 50010;
const int MAX_J = 20;

int nMax[MAX_I][MAX_J];
int nMin[MAX_I][MAX_J];
int nArr[MAX_I];
int nN, nQ;

void InitRmq(int nN)
{
    for (int i = 1; i <= nN; ++i)
    {
        nMax[i][0] = nMin[i][0] = nArr[i];
    }
    
    for (int j = 1; (1 << j) <= nN; ++j)
    {
        for (int i = 1; i + (1 << j) - 1 <= nN; ++i)
        {
            nMax[i][j] = max(nMax[i][j - 1],
                             nMax[i + (1 << (j - 1))][j - 1]);
            nMin[i][j] = min(nMin[i][j - 1],
                             nMin[i + (1 << (j - 1))][j - 1]);                
        }
    }
}

int Query(int nA, int nB)
{
    int k = (int)(log(1.0 * nB - nA + 1) / log(2.0));
    int nBig = max(nMax[nA][k], nMax[nB - (1 << k) + 1][k]);
    int nSml = min(nMin[nA][k], nMin[nB - (1 << k) + 1][k]);
    return nBig - nSml;
}

int main()
{
    while (scanf("%d%d", &nN, &nQ) == 2)
    {
        for (int i = 1; i <= nN; ++i)
        {
            scanf("%d", &nArr[i]);
        }
        InitRmq(nN);
        for (int i = 0; i < nQ; ++i)
        {
            int nA, nB;
            scanf("%d%d", &nA, &nB);
            printf("%d\n", Query(nA, nB));
        }
    }
    
    return 0;
}


yx 2012-10-25 19:29 发表评论
]]>
hdu 3068 最长回?Manacher法http://m.shnenglu.com/csu-yx-2013/archive/2012/10/24/193807.htmlyxyxWed, 24 Oct 2012 12:55:00 GMThttp://m.shnenglu.com/csu-yx-2013/archive/2012/10/24/193807.htmlhttp://m.shnenglu.com/csu-yx-2013/comments/193807.htmlhttp://m.shnenglu.com/csu-yx-2013/archive/2012/10/24/193807.html#Feedback0http://m.shnenglu.com/csu-yx-2013/comments/commentRss/193807.htmlhttp://m.shnenglu.com/csu-yx-2013/services/trackbacks/193807.html   该题貌似可以用后~数组和扩展kmp做,但是好像后缀数组貌似?x)tleQ改学了(jin)?br />一个专门的叫Manacher法的东ѝ。?br />   q又是一个线性改良算法。找到有文章写的不错,链接如下Q?br />http://www.felix021.com/blog/read.php?2040?br />   该算法说h也不是太复杂Q比较容易看懂的那种Q当然是接触q其它字W串法
的前提下?jin)。记得以前就看了(jin)看,是没看懂,想不到现在这么快明白了(jin)?br />   该算法需要额外的O(N)I间。说h是空间换旉吧?br />   大概的思\是先预处理字W串Q其成Z个长度一定ؓ(f)偶数的串。而且W一个字W?br />?$'Q假?$'没有在原串出现过。然后再在原来的每个字符前面加上'#'Q最后再加个
'#'。比如,abc变成了(jin)$#a#b#c#。现在再Ҏ(gu)的字W串q行处理?br />   开一个新的数lnRad[MAX]QnRad[i]表示C中第i个位|向左边和向双同时扩展
q且保持对称的最大距R如果求Z(jin)nRad数组后,有一个结论,nRad[i]-1恰好表示原串
对应的位|能够扩展的回文子串长度。这个的证明Q应该比较简单,因ؓ(f)C基本上是原串
?倍了(jin)Q而且C每一个有效字W两侧都有插入的#Q这个找个例子看下就知道是这样了(jin)?br />   最重要的是如何求出nRad数组?br />   求这个数l的法也主要是利用?jin)一些间接的l论优化?jin)nRad[i]的初始化倹{比如我们求
nRad[i]的时候,如果知道?jin)i以前的nRad|而且知道?jin)前面有一个位|idQ能够最大的?br />两边扩展距离max。那么有一个结论,nRad[i] 能够初始化ؓ(f)min(nRad[2*id - i], max - i)Q?br />然后再进行递增。关键是如何证明q个Q这个的证明Q对照图片就很清楚了(jin)?br />   证明如下Q?br />   ?mx - i > P[j] 的时候,以S[j]Z?j)的回文子串包含在以S[id]Z?j)的回文子串中,׃?i ?j 对称Q?br />以S[i]Z?j)的回文子串必然包含在以S[id]Z?j)的回文子串中,所以必?P[i] = P[j]Q见下图?br />    
   
   ?P[j] > mx - i 的时候,以S[j]Z?j)的回文子串不完全包含于以S[id]Z?j)的回文子串中,但是Z
对称性可知,下图中两个绿框所包围的部分是相同的,也就是说以S[i]Z?j)的回文子串Q其向右臛_?br />扩张到mx的位|,也就是说 P[i] >= mx - i。至于mx之后的部分是否对Uͼ只能老老实实去匚w?jin)?br />
   
      q个p明得很清楚了(jin)。。?br />
      代码如下Q?br />
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int MAX = 110010 * 2;
char szIn[MAX];
char szOut[MAX];
int nRad[MAX];

int Proc(char* pszIn, char* pszOut)
{
    int nLen = 1;
    
    *pszOut++ = '$';
    while (*pszIn)
    {
        *pszOut++ = '#';
        nLen++;
        *pszOut++ = *pszIn++;
        nLen++;
    }
    *pszOut++ = '#';
    *pszOut = '\0';
    
    return nLen + 1;
}

void Manacher(int* pnRad, char* pszStr, int nN)
{
    int nId = 0, nMax = 0;
    
    //pnRad[0] = 1;
    for (int i = 0; i < nN; ++i)
    {
        if (nMax > i)
        {
            pnRad[i] = min(pnRad[2 * nId - i], nMax - i);
        }
        else pnRad[i] = 1;
        
        while (pszStr[i + pnRad[i]] == pszStr[i - pnRad[i]])
        {
            ++pnRad[i];
        }
        if (pnRad[i] + i > nMax)
        {
            nMax = pnRad[i] + i;
            nId = i;
        }
    }
}

int main()
{
    while (scanf("%s", szIn) == 1)
    {
        int nLen = Proc(szIn, szOut);
        Manacher(nRad, szOut, nLen);
        int nAns = 1;
        for (int i = 0; i < nLen; ++i)
        {
            nAns = max(nRad[i], nAns);
        }
        printf("%d\n", nAns - 1);
    }
    
    return 0;
}


yx 2012-10-24 20:55 发表评论
]]>
poj 3294 Life Forms 后缀数组求至出现在K个字W串中的最长公共子?/title><link>http://m.shnenglu.com/csu-yx-2013/archive/2012/10/24/193783.html</link><dc:creator>yx</dc:creator><author>yx</author><pubDate>Wed, 24 Oct 2012 07:57:00 GMT</pubDate><guid>http://m.shnenglu.com/csu-yx-2013/archive/2012/10/24/193783.html</guid><wfw:comment>http://m.shnenglu.com/csu-yx-2013/comments/193783.html</wfw:comment><comments>http://m.shnenglu.com/csu-yx-2013/archive/2012/10/24/193783.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/csu-yx-2013/comments/commentRss/193783.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/csu-yx-2013/services/trackbacks/193783.html</trackback:ping><description><![CDATA[   此题是l出N个字W串Q然后求一个最长的子串Q它臛_出现在N/2+1个字W串中,<br />如果有多个这L(fng)子串Q按字典序输出,如果没有q样的子Ԍ输出??br />   此题是罗I骞论文里面的例11Q他有讲q具体的解法。要用后~数组做这L(fng)题真?br />Ҏ(gu)Q用后缀数组感觉是一仉常纠l的事情?jin)?br />   q个题的解法q是那种模式化的思\。把N个字W串q接成一个,注意中间加不出现?br />M一个字W串中的分隔W,然后建立sa数组和height数组{?br />   最后二分答案,Ҏ(gu){案Q即子串的长度对height数组q行分组Q分l的思\q是|穗<br />骞论文里面例3的思\Q即从到后枚举height数组Q把q箋大于{于{案的值放做一l,<br />一旦小于答案那么就是新的分l。这个题需要找C些分l,其中的后~是能够出现在N个原<br />串中Q这个分l的公共前缀是sa[i]开始的nMid个字W了(jin)(nMid是二分时候获得的子串长度)?br />   ׃q个题需要按字典序输出多个满求的子串Q所以麻?ch)?jin)炏V需要在Check函数里面<br />记录q些子串Q而且输出{案的时候需要排序,再uniqueQ由于是按height数组的顺序查扄Q?br />而sa[i]已经排好序了(jin)Q所以排序答案的q程可以省略Q但是必unique。想下Check函数里面<br />遍历height数组的过E就知道可能出现重复的子丌Ӏ。?br /><br />   代码如下Q?br /><div style="background-color: #eeeeee; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all; "><div>#include <stdio.h></div><div>#include <string.h></div><div>#include <algorithm></div><div>using namespace std;</div><div></div><div>const int MAX_N = 110;</div><div>const int MAX_L = 1010;</div><div>const int MAX = MAX_N * MAX_L;</div><div></div><div>int nAns;</div><div>char szStr[MAX_L];</div><div>char szAns[MAX][MAX_L];</div><div>char* pszAns[MAX];</div><div>int nNum[MAX];</div><div>int nLoc[MAX];</div><div>bool bVis[MAX_N];</div><div>int sa[MAX], rank[MAX], height[MAX];</div><div>int wa[MAX], wb[MAX], wv[MAX], wd[MAX];</div><div></div><div>bool CmpStr(const char* pszOne, const char* pszTwo)</div><div>{</div><div>    return strcmp(pszOne, pszTwo) < 0;</div><div>}</div><div></div><div>bool EqualStr(const char* pszOne, const char* pszTwo)</div><div>{</div><div>    return strcmp(pszOne, pszTwo) == 0;</div><div>}</div><div></div><div>int cmp(int* r, int a, int b, int l)</div><div>{</div><div>    return r[a] == r[b] && r[a + l] == r[b + l];</div><div>}</div><div></div><div>//倍增法,r为待匚w数组,n为总长?m为字W串范围</div><div>void da(int* r, int n, int m)</div><div>{</div><div>    int i, j, p, *x = wa, *y = wb;</div><div>    </div><div>    for (i = 0; i < m; ++i) wd[i] = 0;</div><div>    for (i = 0; i < n; ++i) wd[x[i] = r[i]]++;</div><div>    for (i = 1; i < m; ++i) wd[i] += wd[i - 1];</div><div>    for (i = n - 1; i >= 0; --i) sa[--wd[x[i]]] = i;</div><div>    </div><div>    for (j = 1, p = 1; p < n; j *= 2, m = p)</div><div>    {</div><div>        for (p = 0, i = n - j; i < n; ++i) y[p++] = i;</div><div>        for (i = 0; i < n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j;</div><div>        </div><div>        for (i = 0; i < n; ++i) wv[i] = x[y[i]];</div><div>        for (i = 0; i < m; ++i) wd[i] = 0;</div><div>        for (i = 0; i < n; ++i) wd[wv[i]]++;</div><div>        for (i = 1; i < m; ++i) wd[i] += wd[i - 1];</div><div>        for (i = n - 1; i >= 0; --i) sa[--wd[wv[i]]] = y[i];</div><div>        </div><div>        swap(x, y);</div><div>        for (p = 1, x[sa[0]] = 0, i = 1; i < n; ++i)</div><div>        {</div><div>            x[sa[i]] = cmp(y, sa[i - 1], sa[i], j)? p - 1 : p++;</div><div>        }</div><div>    }</div><div>}</div><div></div><div>//求height数组</div><div>void calHeight(int* r, int n)</div><div>{</div><div>    int i, j, k = 0;</div><div>    for (i = 1; i <= n; ++i) rank[sa[i]] = i;</div><div>    for (i = 0; i < n; height[rank[i++]] = k)</div><div>    {</div><div>        if (k) --k;</div><div>        for(j = sa[rank[i] - 1]; r[i + k] == r[j + k]; k++);</div><div>    }</div><div>}</div><div></div><div>bool Check(int nMid, int nN, int nK)</div><div>{</div><div>    int nCnt = 0;</div><div>    int nNo = 0;</div><div>    </div><div>    memset(bVis, false, sizeof(bVis));</div><div>    for (int i = 1; i <= nN; ++i)</div><div>    {</div><div>        if (height[i] < nMid)</div><div>        {</div><div>            nCnt = 0;</div><div>            memset(bVis, false, sizeof(bVis));</div><div>        }</div><div>        else</div><div>        {</div><div>            if (!bVis[nLoc[sa[i - 1]]])</div><div>            {</div><div>                ++nCnt;</div><div>                bVis[nLoc[sa[i - 1]]] = true;</div><div>            }</div><div>            if (!bVis[nLoc[sa[i]]])</div><div>            {</div><div>                ++nCnt;</div><div>                bVis[nLoc[sa[i]]] = true;</div><div>            }</div><div>            if (nCnt == nK)</div><div>            {</div><div>                for (int j = 0; j < nMid; ++j)</div><div>                {</div><div>                    szAns[nNo][j] = nNum[sa[i] + j];</div><div>                }</div><div>                szAns[nNo][nMid] = 0;</div><div>                ++nNo;</div><div>                nCnt = 0;</div><div>            }</div><div>        }</div><div>    }</div><div>    </div><div>    if (nNo > 0) nAns = nNo;</div><div>    return nNo > 0;</div><div>}</div><div></div><div>int main()</div><div>{</div><div>    int nN;</div><div>    bool bFirst = true;</div><div>    </div><div>    while (scanf("%d", &nN), nN)</div><div>    {</div><div>        if (bFirst) bFirst = false;</div><div>        else putchar('\n');</div><div>        </div><div>        int nEnd = 300;</div><div>        int nP = 0;</div><div>        for (int i = 0; i < nN; ++i)</div><div>        {</div><div>            scanf("%s", szStr);</div><div>            int nLen = strlen(szStr);</div><div>            for (int j = 0; j < nLen; ++j)</div><div>            {</div><div>                nNum[nP] = szStr[j];</div><div>                nLoc[nP++] = i;</div><div>            }</div><div>            nNum[nP] = nEnd;</div><div>            nLoc[nP++] = nEnd++;</div><div>        }</div><div>        nNum[nP] = 0;</div><div>        </div><div>        if (nN == 1)</div><div>        {</div><div>            printf("%s\n\n", szStr);</div><div>            continue;</div><div>        }</div><div>        da(nNum, nP + 1, 500);//500是估计的字符集大?/div><div>        calHeight(nNum, nP);</div><div>        </div><div>        int nLeft = 1, nRight = strlen(szStr);</div><div>        int nTemp = 0, nMid;</div><div>        int nK = nN / 2 + 1;</div><div>        nAns = 0;</div><div>        while (nLeft <= nRight)</div><div>        {</div><div>            nMid = (nLeft + nRight) >> 1;</div><div>            if (Check(nMid, nP, nK))</div><div>            {</div><div>                nTemp = nMid;</div><div>                nLeft = nMid + 1;</div><div>            }</div><div>            else nRight = nMid - 1;</div><div>        }</div><div>        if (nTemp == 0)</div><div>        {</div><div>            printf("?\n");</div><div>        }</div><div>        else</div><div>        {</div><div>            for (int i = 0; i < nAns; ++i)</div><div>            {</div><div>                pszAns[i] = szAns[i];</div><div>            }</div><div>            //sort(pszAns, pszAns + nAns, CmpStr);</div><div>            nAns = unique(pszAns, pszAns + nAns, EqualStr) - pszAns;</div><div>            for (int i = 0; i < nAns; ++i)</div><div>            {</div><div>                printf("%s\n", pszAns[i]);</div><div>            }</div><div>        }</div><div>    }</div><div>    </div><div>    return 0;</div><div>}</div><div style="font-size: 13px; "></div></div><img src ="http://m.shnenglu.com/csu-yx-2013/aggbug/193783.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/csu-yx-2013/" target="_blank">yx</a> 2012-10-24 15:57 <a href="http://m.shnenglu.com/csu-yx-2013/archive/2012/10/24/193783.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>poj 1226 Substrings 后缀数组http://m.shnenglu.com/csu-yx-2013/archive/2012/10/23/193741.htmlyxyxTue, 23 Oct 2012 13:11:00 GMThttp://m.shnenglu.com/csu-yx-2013/archive/2012/10/23/193741.htmlhttp://m.shnenglu.com/csu-yx-2013/comments/193741.htmlhttp://m.shnenglu.com/csu-yx-2013/archive/2012/10/23/193741.html#Feedback0http://m.shnenglu.com/csu-yx-2013/comments/commentRss/193741.htmlhttp://m.shnenglu.com/csu-yx-2013/services/trackbacks/193741.html初学后缀数组Q有很多不明白的东西Q此题后~数组的代码在|上也是一把抓?br />   说实话我实q不懂后~数组Q但是后~数组太强大了(jin)Q只能硬着头皮照着葫芦ȝ?jin)?br />贴下代码方便以后查阅吧。。?br />   感觉后缀数组的应用最主要的还是height数组Q看懂倍增法排序后缀已经非常困难?jin)?br />然后再理解height数组怎么用也不是一件容易的事情。然后貌似height数组最关键的用法是
枚D某一个长度的子串时候,比如长度为kQ能够用q个k对height数组q行分组Q这个罗I骞
的论文里面有个求不重叠最镉K复子串的例子说明?jin)这个height数组分组的思\Q不q我现在
q是不怎么理解。。?br />  
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int MAX_N = 110;
const int MAX_L = MAX_N * MAX_N;
char szStr[MAX_N];
int nNum[MAX_L];
int nLoc[MAX_L];
bool bVisit[MAX_N];
int sa[MAX_L], rank[MAX_L], height[MAX_L];
int wa[MAX_L], wb[MAX_L], wv[MAX_L], wd[MAX_L];

int cmp(int* r, int a, int b, int l)
{
    return r[a] == r[b] && r[a + l] == r[b + l];
}

//倍增法,r为待匚w数组,n为总长?m为字W串范围
void da(int* r, int n, int m)
{
    int i, j, p, *x = wa, *y = wb;
    
    for (i = 0; i < m; ++i) wd[i] = 0;
    for (i = 0; i < n; ++i) wd[x[i] = r[i]]++;
    for (i = 1; i < m; ++i) wd[i] += wd[i - 1];
    for (i = n - 1; i >= 0; --i) sa[--wd[x[i]]] = i;
    
    for (j = 1, p = 1; p < n; j *= 2, m = p)
    {
        for (p = 0, i = n - j; i < n; ++i) y[p++] = i;
        for (i = 0; i < n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j;
        
        for (i = 0; i < n; ++i) wv[i] = x[y[i]];
        for (i = 0; i < m; ++i) wd[i] = 0;
        for (i = 0; i < n; ++i) wd[wv[i]]++;
        for (i = 1; i < m; ++i) wd[i] += wd[i - 1];
        for (i = n - 1; i >= 0; --i) sa[--wd[wv[i]]] = y[i];
        
        swap(x, y);
        for (p = 1, x[sa[0]] = 0, i = 1; i < n; ++i)
        {
            x[sa[i]] = cmp(y, sa[i - 1], sa[i], j)? p - 1 : p++;
        }
    }
}

//求height数组
void calHeight(int* r, int n)
{
    int i, j, k = 0;
    for (i = 1; i <= n; ++i) rank[sa[i]] = i;
    for (i = 0; i < n; height[rank[i++]] = k)
    {
        if (k) --k;
        for(j = sa[rank[i] - 1]; r[i + k] == r[j + k]; k++);
    }
}

bool Check(int nMid, int nLen, int nN)
{
    int nCnt = 0;
    
    memset(bVisit, falsesizeof(bVisit));
    for (int i = 2; i <= nLen; ++i)
    {
        if (nMid > height[i])
        {
            nCnt = 0;
            memset(bVisit, falsesizeof(bVisit));
            continue;
        }
        if (!bVisit[nLoc[sa[i - 1]]])
        {
            bVisit[nLoc[sa[i - 1]]] = true;
            ++nCnt;
        }
        if (!bVisit[nLoc[sa[i]]])
        {
            bVisit[nLoc[sa[i]]] = true;
            ++nCnt;
        }
        if (nCnt == nN) return true;
    }
    
    return false;
}

int main()
{
    int nT;
    
    scanf("%d", &nT);
    while (nT--)
    {
        int nN;
        int nEnd = 300;
        int nP = 0;
        scanf("%d", &nN);
        for (int i = 1; i <= nN; ++i)
        {
            scanf("%s", szStr);
            char* pszStr;
            for (pszStr = szStr; *pszStr; ++pszStr)
            {
                nLoc[nP] = i;
                nNum[nP++] = *pszStr;
            }
            nLoc[nP] = nEnd;
            nNum[nP++] = nEnd++;
            
            reverse(szStr, szStr + strlen(szStr));
            for (pszStr = szStr; *pszStr; ++pszStr)
            {
                nLoc[nP] = i;
                nNum[nP++] = *pszStr;
            }
            nLoc[nP] = nEnd;
            nNum[nP++] = nEnd++;
        }
        nNum[nP] = 0;
        
        da(nNum, nP + 1, nEnd);
        calHeight(nNum, nP);
        
        int nLeft = 1, nRight = strlen(szStr), nMid;
        int nAns = 0;
        while (nLeft <= nRight)
        {
            nMid = (nLeft + nRight) / 2;
            if (Check(nMid, nP, nN))
            {
                nLeft = nMid + 1;
                nAns = nMid;
            }
            else nRight = nMid - 1;
        }
        printf("%d\n", nAns);
    }
    
    return 0;
}


yx 2012-10-23 21:11 发表评论
]]>
poj 3691 DNA repair AC自动?+ dphttp://m.shnenglu.com/csu-yx-2013/archive/2012/10/21/193619.htmlyxyxSun, 21 Oct 2012 08:53:00 GMThttp://m.shnenglu.com/csu-yx-2013/archive/2012/10/21/193619.htmlhttp://m.shnenglu.com/csu-yx-2013/comments/193619.htmlhttp://m.shnenglu.com/csu-yx-2013/archive/2012/10/21/193619.html#Feedback0http://m.shnenglu.com/csu-yx-2013/comments/commentRss/193619.htmlhttp://m.shnenglu.com/csu-yx-2013/services/trackbacks/193619.html可以使文本串不包含Q何一个模式串?br />   q是先徏立Trie图,然后在Trie图上面进行dp。dp的思\也不是很复杂。dp[i][j]的意?br />是长度ؓ(f)i的文本串需要改变dp[i][j]个字W顺利到辄态j。需要注意的是长度ؓ(f)i的时候,
对应的字W串中的Wi-1个字W。刚开始一直没发现q个bug。而且注意中途不能{Ud
匚w成功的状态上去,多加几个条g控制卛_?jin)。。?br />   转移方程Qdp[i][j] = min(dp[i][j], dp[i-1][nNext] + szText[i-1] != k)Q其中nNext
是从状态j可以转移到的非匹配成功的状态,k代表的当前边的权?br />   
   代码如下Q?br />
#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是边权树(wi),所以i是从1到l(f)en,而且当前字符是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;
}


yx 2012-10-21 16:53 发表评论
]]>
poj 1625 Censored! AC自动?+ DP + 大数加法http://m.shnenglu.com/csu-yx-2013/archive/2012/10/20/193579.htmlyxyxSat, 20 Oct 2012 13:01:00 GMThttp://m.shnenglu.com/csu-yx-2013/archive/2012/10/20/193579.htmlhttp://m.shnenglu.com/csu-yx-2013/comments/193579.htmlhttp://m.shnenglu.com/csu-yx-2013/archive/2012/10/20/193579.html#Feedback0http://m.shnenglu.com/csu-yx-2013/comments/commentRss/193579.htmlhttp://m.shnenglu.com/csu-yx-2013/services/trackbacks/193579.html而且文本串不太长。问题是不取模的话就只能输出实际的答案了(jin)Q就只能用大C(jin)?br />   而且用大数的话,再用矩阵冥可能就?x)超时之cȝ?br />   q类题还可以用除矩阵冥外的另外一U解法,是直接dp卛_?br />   二维状态,W一l代表文本串长度Q第二维代表在AC自动Z的状态?br />   比如dp[i][j]代表长度为i的文本串Q{UdTrie图中节点j时候满不包含M模式串的{案?br />剩下的是如何转移状态。{Uȝ话也是考虑next指针数组Q设next = tries[j].next[k]Q?br />那么有dp[i+1][next] = dp[i+1][next] + dp[i][j]Q从0到字母集合大N枚Dk卛_?br />   q个题有一个易错的地方Q就是字母集合可能是ascii码在128?56的范围内。而char
的范围可能是-128?27或??55Q这个是Ҏ(gu)~译器不同的。所以,直接用字W串
数组d数据后需要再处理下。可以直接将每个字符?28后再处理?br />   另外Qgetcharq回的是intQ但是与gets之类的函数获得的值的差别也不是那么确定的?jin)?br />我觉得getchar除了(jin)对eof之外其余都返回正倹{但是,如果char是有W号的话Qscanf或?br />gets之类得到的char数组里面可能包含负g(jin)。。?br />   q个可以生成随机文gQ再用getchardq用%d输出其返回值验证下。验证程序如下:(x)
注释掉的部分是生成随机文件的部分?br />
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char ch;
    freopen("in.txt", "r", stdin);
    //freopen("in.txt", "w", stdout);
    int nNum = 100;
    int nCh;
    do
    {
        printf("%d\n", nCh = getchar());
    }while (nCh != EOF);
    /*while (nNum--)
    {
        putchar(rand() % 256);
    }
*/
    
    return 0;
}
   
   该题的代码如下:(x)
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;

const int MAX_D = 256;
const int MAX_N = 51;
const int MAX_M = 51;
const int MAX_P = 11;
struct Trie
{
    Trie* fail;
    Trie* next[MAX_D];
    int no;
    bool flag;
};
Trie tries[MAX_P * MAX_P];
int nP;
int nN, nM;
Trie* pRoot;
int nHash[MAX_D];
char szPat[MAX_M];

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 = nHash[*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 < nN; ++i)
        {
            if (front->next[i])
            {
                Trie* pNode = front;
                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->fail->next[i];
            }
        }
    }
}

const int MAX_L = 200;
struct BigInt
{
    int nD[MAX_L];
    BigInt()
    {
        Clear();
    }
    void Clear()
    {
        memset(nD, 0, sizeof(nD));
    }
    
    void Print()
    {
        int i = MAX_L - 1;
        while (!nD[i] && i)--i;
        while (i >= 0)
        {
            putchar(nD[i] + '0');
            --i;
        }
    }
    int operator[](int idx) const
    {
        return nD[idx];
    }
    
    intoperator[](int idx)
    {
        return nD[idx];
    }
};
BigInt bi[MAX_M][MAX_D];

BigInt operator+(const BigInt& one, const BigInt& two)
{
    BigInt ret;
    
    for (int i = 0, nAdd = 0; i < MAX_L; ++i)
    {
        ret[i] = one[i] + two[i] + nAdd;
        nAdd = ret[i] / 10;
        ret[i] %= 10;
    }
    
    return ret;
}

void Solve()
{
    BigInt ans;
    for (int i = 0; i <= nM; ++i)
    {
        for (int j = 0; j < nP; ++j)
        {
            bi[i][j].Clear();
        }
    }
    bi[0][0][0] = 1;
    
    for (int i = 1; i <= nM; ++i)
    {
        for (int j = 0; j < nP; ++j)
        {
            if (tries[j].flag) continue;
            for (int k = 0; k < nN; ++k)
            {
                int nNext = tries[j].next[k] - tries;
                if (tries[nNext].flag == false)
                {
                    bi[i][nNext] = bi[i][nNext] + bi[i - 1][j];
                }
            }
        }
    }
    
    for (int i = 0; i < nP; ++i)
    {
        ans = ans + bi[nM][i];
    }
    ans.Print();
    printf("\n");
}

int main()
{
    int nT;
    
    while (scanf("%d%d%d%*c", &nN, &nM, &nT) == 3)
    {
        int nCh;
        int nTmp = 0;
        memset(nHash, 0, sizeof(nHash));
        while (nCh = getchar(), nCh != '\n')
        {
            if (!nHash[nCh])
            {
                nHash[nCh] = nTmp++;
            }
        }
        InitTrie(pRoot);
        while (nT--)
        {
            gets(szPat);
            Insert(pRoot, szPat);
        }
        printf("1");
        BuildAC(pRoot);
        printf("2");
        Solve();
    }
    
    return 0;
}


yx 2012-10-20 21:01 发表评论
]]>
poj 1509 Glass Beads 字符串最表C?/title><link>http://m.shnenglu.com/csu-yx-2013/archive/2012/10/19/193541.html</link><dc:creator>yx</dc:creator><author>yx</author><pubDate>Fri, 19 Oct 2012 11:44:00 GMT</pubDate><guid>http://m.shnenglu.com/csu-yx-2013/archive/2012/10/19/193541.html</guid><wfw:comment>http://m.shnenglu.com/csu-yx-2013/comments/193541.html</wfw:comment><comments>http://m.shnenglu.com/csu-yx-2013/archive/2012/10/19/193541.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.shnenglu.com/csu-yx-2013/comments/commentRss/193541.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/csu-yx-2013/services/trackbacks/193541.html</trackback:ping><description><![CDATA[   赤裸裸的字符串最表C题。所谓字W串最表C指的是l定一个字W串Q假讑օ可以循环U?br />位,问@环左Ud位能够得到最的字符丌Ӏ?br />   法x周源的最表C法Q搜索可以找到相兌文和ppt?br />   该算法其实也不是太复杂,思\可以q样理解。假讑֎字符串ؓ(f)sQ设s1 = s + s; s2 = s1?br />环左U??现在处理s1和s2Q实际写E序的时候可以通过下标偏移和取模得到s1和s2Q而ƈ不需<br />要生成?br />   处理q程是这L(fng)Q设i和j分别指向s1和s2的开头。我们的目的是找到这L(fng)i和jQ假设k是s?br />长度Q满x件s1[i,i+k-1] = s2[j,j+k-1] q且s1[i,i+k-1] 是所有满x件的字符串中最的<br />字符Ԍ如果有多个这L(fng)s1[i,i+k-1] 那么我们希望i最?br />   其实q个法主要是做?jin)一个优化,从而把旉搞成U性的。比如,对于当前的i和jQ我们一?br />q行匚wQ也是s1[i,i+k] = s2[j,j+k] 一直满IH然C(jin)一个位|s1[i+k]  != s2[j+k]?jin)?br />现在我们需要改变i和j?jin)。但是,我们不能只是++i或?+j。而是Ҏ(gu)s1[i+k]>s2[j+k]的话i = <br />i + k + 1Q否则j = j + k + 1。这L(fng)瞬移i或者jp够保证复杂度是线性的?jin)?br />   问题是如何证明可以这L(fng)瞬移。其实,说穿?jin)也很简单。因为s1[i,i+k - 1] = s2[j,j+k -1]<br />是满的Q只是到?jin)s1[i+k]和s2[j+k]才出现问题了(jin)。假如s1[i+k]>s2[j+k]Q那么我们改变i?br />区间[i+1,i+k]中Q何一个值m都不可能得到我们惌的答案,q是因ؓ(f)我们d以在s2中找到相?br />的比s1[m,m+k-1]的字符串s2[j+m-i,j+m-i+k-1]Q因为有s1[i+k]>s2[j+k]?br />   同样对于s1[i+k]<s2[j+k]的情c(din)?br />   文字可能描述的不是很清楚。看PPT能够Ҏ(gu)图进行分析?br /><br />   代码如下Q?br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->#include <stdio.h><br />#include <stdlib.h><br />#include <<span style="color: #0000FF; ">string</span>.h><br />#include <algorithm><br />#include <<span style="color: #0000FF; ">string</span>><br />#include <iostream><br /><span style="color: #0000FF; ">using</span> <span style="color: #0000FF; ">namespace</span> std;<br /><br /><span style="color: #0000FF; ">int</span> GetMin(<span style="color: #0000FF; ">string</span>& str)<br />{<br />    <span style="color: #0000FF; ">int</span> nSize = str.size();<br />    <span style="color: #0000FF; ">int</span> i = 0, j = 1, k = 0;<br />    <br />    <span style="color: #0000FF; ">while</span> (i < nSize && j < nSize && k < nSize)<br />    {<br />        <span style="color: #0000FF; ">char</span> chDif = str[(i + k) % nSize]<br />                    - str[(j + k) % nSize];<br />        <span style="color: #0000FF; ">if</span> (!chDif) ++k;<br />        <span style="color: #0000FF; ">else</span><br />        {<br />            <span style="color: #0000FF; ">if</span> (chDif > 0) i = i + k + 1;<br />            <span style="color: #0000FF; ">else</span> j = j + k + 1;<br />            <span style="color: #0000FF; ">if</span> (i == j) ++j;<br />            k = 0;<br />        }<br />    }<br />    <span style="color: #0000FF; ">return</span> min(i, j);<br />}<br /><br /><span style="color: #0000FF; ">int</span> main()<br />{<br />    <span style="color: #0000FF; ">string</span> str;<br />    <span style="color: #0000FF; ">int</span> nN;<br />    <br />    scanf("%d", &nN);<br />    <span style="color: #0000FF; ">while</span> (nN--)<br />    {<br />        cin >> str;<br />        printf("%d\n", GetMin(str) + 1);<br />    }<br />    <br />    <span style="color: #0000FF; ">return</span> 0;<br />}</div><img src ="http://m.shnenglu.com/csu-yx-2013/aggbug/193541.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/csu-yx-2013/" target="_blank">yx</a> 2012-10-19 19:44 <a href="http://m.shnenglu.com/csu-yx-2013/archive/2012/10/19/193541.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hnu 2243 考研路茫茫——单词情l?AC自动?矩阵冥篏加和http://m.shnenglu.com/csu-yx-2013/archive/2012/10/18/193489.htmlyxyxThu, 18 Oct 2012 14:02:00 GMThttp://m.shnenglu.com/csu-yx-2013/archive/2012/10/18/193489.htmlhttp://m.shnenglu.com/csu-yx-2013/comments/193489.htmlhttp://m.shnenglu.com/csu-yx-2013/archive/2012/10/18/193489.html#Feedback0http://m.shnenglu.com/csu-yx-2013/comments/commentRss/193489.htmlhttp://m.shnenglu.com/csu-yx-2013/services/trackbacks/193489.html   题意是给定M个模式串Q然后给定长度LQ问不超qL的文本至含有一个模式的情况的ȝ数?br />
   q是用模式串建立Trie图,Ҏ(gu)Trie囑־立v路径长度?的矩阵M?br />   L冉|目ؓ(f)26^1+26^2+...+26^L。不含模式串的情冉|L为矩阵N = M^1+M^2+M^3
+...+M^L的第一行之和。L冉|目减M含模式串的情况就是答案?br />   q里用到?jin)矩늚一些算法,比如快速冥Q还有快速冥求和。但是,我用?jin)操作符重蝲Q最(zhn)剧
的是重蝲后的操作W没有优先Q而我q当作有优先U的在用Q所以?zhn)剧?jin)。。。一直样例都q不
厅R。。唉Q最后才发现?jin)这个问题。。。写?60行左右的代码Q前面的一部分代码可以当作?br />阉|作的模板?jin)。。。Trie囄也不错,q几天估计得打印下来用了(jin)。。?br />
   代码如下Q?br />
#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);//重蝲q算W保证不?jin)优先?/span>
    if (nN % 2)
    {
        ans = ans + (base^nN);//没优先?必须加括?查错2个小时了(jin)
    }
    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];
            }

            //q里必须要加上front->flag为false的判断么?加不加会(x)生成不同的矩?/span>
            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;
}


yx 2012-10-18 22:02 发表评论
]]>
poj 2778 DNA Sequence AC自动?矩阵快速冥http://m.shnenglu.com/csu-yx-2013/archive/2012/10/18/193454.htmlyxyxThu, 18 Oct 2012 01:46:00 GMThttp://m.shnenglu.com/csu-yx-2013/archive/2012/10/18/193454.htmlhttp://m.shnenglu.com/csu-yx-2013/comments/193454.htmlhttp://m.shnenglu.com/csu-yx-2013/archive/2012/10/18/193454.html#Feedback0http://m.shnenglu.com/csu-yx-2013/comments/commentRss/193454.htmlhttp://m.shnenglu.com/csu-yx-2013/services/trackbacks/193454.html串的可能性到底有多少U。。?br />   实非常不直观的样子。。?br />   解法是先学学AC自动机,建立起Trie图,Ҏ(gu)trie囑֏以得到长度ؓ(f)1的\径矩阵,然后再快?br />冥得到长度ؓ(f)N的\径矩c(din)?br />   说v来都非常U结Q没学过AC自动机更加无法理解。学AC自动Z前据说得先学Trie?wi)和KMP
才好理解。学AC自动机搞Trie囑ְp?jin)?天了(jin)Q然后弄懂这个题又是一天,好在基本明白?jin)?br />   马上快比赛了(jin)Q从长春换到金华也不知道是好是坏。。。还是弱菜啊。。?br />   贴下我的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;//节点M?/span>
}

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;
}
   
   

yx 2012-10-18 09:46 发表评论
]]>
hnu 10076 Jimmy's Riddles DFAhttp://m.shnenglu.com/csu-yx-2013/archive/2012/10/12/193227.htmlyxyxFri, 12 Oct 2012 14:14:00 GMThttp://m.shnenglu.com/csu-yx-2013/archive/2012/10/12/193227.htmlhttp://m.shnenglu.com/csu-yx-2013/comments/193227.htmlhttp://m.shnenglu.com/csu-yx-2013/archive/2012/10/12/193227.html#Feedback2http://m.shnenglu.com/csu-yx-2013/comments/commentRss/193227.htmlhttp://m.shnenglu.com/csu-yx-2013/services/trackbacks/193227.html   感觉用dfa只需要保证状态{换图对了(jin)Q基本上׃?x)出bug?jin),但是其它的方法去匚w
q种cM正则表达式的字符串就Ҏ(gu)出错多了(jin)?br />
   癑ֺ癄的DFA定义如下Q?br />      英文全称QDeterministic Finite Automaton, 写:(x)DFA
  DFA定义Q一个确定的有穷自动机(DFAQM是一个五元组QM=QKQ?#931;QfQSQZQ其?br />  ① K是一个有I集Q它的每个元素称Z个状态;
  ② Σ是一个有I字母表Q它的每个元素称Z个输入符P所以也U?#931;入符号字母表Q?br />  ③ f是{换函敎ͼ是K×Σ→K上的映射Q即Q如 fQkiQaQ?kjQ(ki∈KQkj∈KQ就意味着Q?br />当前状态ؓ(f)kiQ输入符为aӞ{换ؓ(f)下一个状态kjQ我们把kjUCki的一个后l状态;
  ④ S ∈ K是唯一的一个初态;
  ⑤ Z⊂K是一个终态集Q终态也U可接受状态或l束状态?/span>

   该题的状态{换图Q?br />   
   现在再根据状态{换图Q写一个模拟{换关pȝ匚w非常方便了(jin)。。?br />   代码如下Q?br />
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;

string strNouns[8] =
{
    "tom", "jerry", "goofy", "mickey",
    "jimmy", "dog", "cat", "mouse"
};

bool IsNoun(string& str)
{
    for (int i = 0; i < 8; ++i)
    {
        if (str == strNouns[i])
        {
            return true;
        }
    }
    return false;
}

bool IsVerb(string& str)
{
    return str == "hate" || str == "love"
            || str == "know" || str == "like"
            || str == "hates" || str == "loves"
            || str == "knows" || str == "likes"; 
}

bool IsArticle(string& str)
{
    return str == "a" || str == "the";
}

bool CheckState(vector<string>& vs)
{
    if (vs.empty()) return false;
    
    int nState = 0;
    for (int i = 0; i < vs.size(); ++i)
    {
        //printf("nState:%d, str:%s\n", nState, vs[i].c_str());
        switch (nState)
        {
            case 0:
                if (IsArticle(vs[i]))
                {
                    nState = 1;
                    break;
                }
                else if (IsNoun(vs[i]))
                {
                    nState = 2;
                    break;
                }
                else
                {
                    return false;
                }
                
            case 1:
                if (IsNoun(vs[i]))
                {
                    nState = 2;
                    break;
                }
                else
                {
                    return false;
                }
                
            case 2:
                if (vs[i] == "and")
                {
                    nState = 0;
                    break;
                }
                else if (IsVerb(vs[i]))
                {
                    nState = 3;
                    break;
                }
                else
                {
                    return false;
                }
                
            case 3:
                if (IsArticle(vs[i]))
                {
                    nState = 4;
                    break;
                }
                else if (IsNoun(vs[i]))
                {
                    nState = 5;
                    break;
                }
                else
                {
                    return false;
                }
                
            case 4:
                if (IsNoun(vs[i]))
                {
                    nState = 5;
                    break;
                }
                else
                {
                    return false;
                }
                
            case 5:
                if (vs[i] == "and")
                {
                    nState = 3;
                    break;
                }
                else if (vs[i] == ",")
                {
                    nState = 0;
                    break;
                }
                else
                {
                    return false;
                }
        }
    }
    
    return nState == 5;
}

int main()
{
    int nT;
    
    scanf("%d%*c", &nT);
    while (nT--)
    {
        vector<string> vs;
        string line, str;
        
        getline(cin, line);
        stringstream ss(line);
        while (ss >> str)
        {
            vs.push_back(str);
        }
        printf("%s\n", CheckState(vs) ? "YES I WILL" : "NO I WON'T");
    }
    
    return 0;
}


yx 2012-10-12 22:14 发表评论
]]>
þ99Ʒþþþþ| Ʒѿþþ㽶| þøƬ| þù| ޾Ʒþþþsm| ޹þþþƷ| ľþþƷww16| ƷþþþþĻ| ŷþۺŷ| ޾ƷĻþò| Ʒ99þþƷ| þþþþþòҰ¸߳| þ99ȺݺɫƷһ| þþþþžžƷӰԺ| 69ۺϾþþƷ| Ʒŷþþþ޹| ŷѹۿþ| 99þþƷۺһ| þþƷ77777| ɫۺϾþþþĻ| Ʒþþþþ12| ղƷþþþþþ| þþþavëƬ| þøһëƬ| þþþAV| ҹƵþþþһ| þþƷ| ޳ɫWWWþվ| ɫþþþۺ| ŵþ| ۺϾþþ| ҹþӰԺ| þþþ޾Ʒ| ɫۺϾþ| ޾ƷҾþþþþ| þþƷ| 㽶þƵ| ŷƷרþ | ƷŮ߳׾þþ| þþƷ޾Ʒ| ݺɫۺվþþþþþ|