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

            為生存而奔跑

               :: 首頁 :: 聯系 :: 聚合  :: 管理
              271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks

            留言簿(5)

            我參與的團隊

            搜索

            •  

            積分與排名

            • 積分 - 330187
            • 排名 - 74

            最新評論

            閱讀排行榜

            評論排行榜

             

              1/*hdu2222*/
              2#include<iostream>
              3#include<algorithm>
              4#include<map>
              5using namespace std;
              6
              7const int MAXQ=500000+10;
              8const int MAXN = 1000000+10;
              9class trie
             10{
             11public:
             12    trie *next[26];
             13    trie *fail;
             14    int count;
             15    trie()
             16    {
             17        for(int i=0;i<26;i++)
             18            next[i]=NULL;
             19        count=0;
             20    }

             21}
            ;
             22trie *root;
             23trie *q[MAXQ];
             24char text[MAXN];
             25void insert(char *s)
             26{
             27    int i,index;
             28    trie *now=root;
             29    i=0;
             30    while(s[i]!='\0')
             31    {
             32        index=s[i]-'a';
             33        if(now->next[index]==NULL)
             34            now->next[index]=new trie;
             35        now=now->next[index];
             36        i++;
             37    }

             38    now->count++;
             39}

             40void build_AC_automation()
             41{    
             42    trie *p,*now;
             43    int top=0,tail=1;
             44    q[0]=root;
             45    root->fail=NULL;
             46    while(top<tail)
             47    {
             48        now=q[top++];
             49        for(int i=0;i<26;i++)
             50            if(now->next[i]!=NULL)
             51            {
             52                if(now==root)
             53                    now->next[i]->fail=root;
             54                else
             55                {
             56                    p=now->fail;
             57                    while(p!=NULL)
             58                    {
             59                        if(p->next[i]!=NULL)
             60                        {
             61                            now->next[i]->fail=p->next[i];
             62                            break;
             63                        }

             64                        p=p->fail;
             65                    }

             66                    if(p==NULL)
             67                        now->next[i]->fail=root;
             68                }

             69                q[tail++]=now->next[i];
             70            }

             71    }

             72}

             73int AC_search()
             74{
             75    trie *cur=root,*p;
             76    int index,ans;
             77    ans=0;
             78    for(int i=0;text[i]!='\0';i++)
             79    {
             80        index=text[i]-'a';
             81        while(cur->next[index]==NULL && cur!=root)
             82            cur=cur->fail;
             83        cur=cur->next[index];
             84        if(cur==NULL)
             85            cur=root;
             86        p=cur;
             87        while(p!=NULL && p->count!=-1)
             88        {
             89            ans+=p->count;
             90            p->count=-1;
             91            p=p->fail;
             92        }

             93    }

             94    return ans;
             95}

             96int main()
             97{
             98    int t,n;
             99    char keyword[52];
            100    scanf("%d",&t);
            101    while(t--)
            102    {
            103        root=new trie();
            104        scanf("%d",&n);
            105        while(n--)
            106        {
            107            scanf("%s",keyword);
            108            insert(keyword);
            109        }

            110    
            111        scanf("%s",text);
            112        build_AC_automation();
            113        printf("%d\n",AC_search());
            114    }

            115}

            116

             

            posted on 2009-07-24 16:45 baby-fly 閱讀(915) 評論(2)  編輯 收藏 引用 所屬分類: Algorithm

            Feedback

            # re: 【AC自動機】HDU 2222 2009-10-16 13:11 july
            你這處理方法不行, 把cout設了-1 他第二次跑到那個點就不會再算了


            看這數據
            1
            2
            ssherr
            he
            ssherrssherr

            你答案是2  回復  更多評論
              

            # re: 【AC自動機】HDU 2222 2009-10-17 11:37 july
            @july
            沒錯,原來他的題目就是只算一次..........  回復  更多評論
              

            少妇久久久久久被弄高潮| 狠狠干狠狠久久| 伊人久久精品影院| 久久精品青青草原伊人| 99久久99这里只有免费费精品| 久久91精品久久91综合| 亚洲精品国产综合久久一线| 久久99精品久久久久久久不卡| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 97精品国产97久久久久久免费| 久久国产亚洲高清观看| 一本色综合久久| 狠狠精品久久久无码中文字幕 | 国产精品无码久久综合 | 久久久久亚洲精品男人的天堂| 久久午夜羞羞影院免费观看| 欧美一级久久久久久久大片| 婷婷综合久久中文字幕| 色综合久久久久综合体桃花网| 久久99精品久久久久久齐齐| 久久精品国产免费| 久久99精品国产自在现线小黄鸭| 色妞色综合久久夜夜| 久久精品亚洲精品国产欧美| 国产精品久久久久…| 国内精品九九久久久精品| 日产精品久久久久久久| 中文精品99久久国产| 四虎影视久久久免费观看| 国产高潮久久免费观看| 热re99久久精品国产99热| 成人免费网站久久久| 久久成人精品视频| 久久国产乱子伦免费精品| 久久99精品久久久久久hb无码| 久久亚洲精品无码AV红樱桃| 国产午夜福利精品久久2021| 国产精品一久久香蕉产线看| 国产午夜精品理论片久久影视| 久久精品国产精品青草| 很黄很污的网站久久mimi色 |