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

            我參與的團隊

            搜索

            •  

            積分與排名

            • 積分 - 328435
            • 排名 - 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 閱讀(910) 評論(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
            沒錯,原來他的題目就是只算一次..........  回復  更多評論
              

            久久久久久久久久久精品尤物| 蜜臀av性久久久久蜜臀aⅴ麻豆| 无码人妻久久久一区二区三区 | 日韩精品久久久久久免费| 久久国产香蕉一区精品| 久久久久这里只有精品| 香蕉99久久国产综合精品宅男自 | 亚洲中文久久精品无码| 国产aⅴ激情无码久久| 97精品依人久久久大香线蕉97| 亚洲国产成人久久综合野外| 怡红院日本一道日本久久| 亚洲一本综合久久| 激情综合色综合久久综合| 久久中文字幕视频、最近更新| 老司机午夜网站国内精品久久久久久久久 | 久久人妻无码中文字幕| 97久久国产综合精品女不卡| 国产精品久久波多野结衣| 精品久久人人爽天天玩人人妻| 久久久精品国产Sm最大网站| 久久天天躁狠狠躁夜夜avapp| 乱亲女H秽乱长久久久| 久久99国产精品久久| 久久天天躁狠狠躁夜夜2020| 亚洲精品午夜国产VA久久成人| 精品蜜臀久久久久99网站| 久久99精品久久久久久不卡 | 久久妇女高潮几次MBA| 久久99精品国产99久久| 久久经典免费视频| 国产精品久久网| 久久人人爽人人爽人人av东京热| 久久九九青青国产精品| 思思久久99热只有频精品66| 99久久精品影院老鸭窝| 亚洲精品成人网久久久久久| 97久久精品无码一区二区天美| 亚洲精品久久久www| 久久精品国产精品亚洲精品| 国产69精品久久久久观看软件|