• <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>
            syhd142  
            日歷
            <2011年2月>
            303112345
            6789101112
            13141516171819
            20212223242526
            272812345
            6789101112
            統計
            • 隨筆 - 23
            • 文章 - 122
            • 評論 - 31
            • 引用 - 0

            導航

            常用鏈接

            留言簿(2)

            隨筆檔案(23)

            文章分類(270)

            文章檔案(122)

            我的豆瓣

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

             
            模板題
            #include <stdio.h>
            #include 
            <string.h>
            #include 
            <math.h>
            #include 
            <vector>
            #include 
            <algorithm>
            #include 
            <queue>
            using namespace std;

            #define KIND 128
            #define M 10010

            struct node
            {
                node 
            *fail;
                node 
            *next[KIND];
                
            int id;
                node ()
                    {
                        fail 
            = NULL;
                        id 
            = 0;
                        memset(next, 
            0sizeof(next));
                    }
            };

            char ch[M];
            queue
            <node *> q;
            vector
            <int> g;

            void insert(node *&root, char *ch, int id)
            {
                node 
            *= root;
                
            int i = 0, t;
                
            while(ch[i])
                {
                    t 
            = ch[i] - 31;
                    
            if(!p->next[t]) p->next[t] = new node();
                    p 
            = p->next[t];
                    i
            ++;
                }
                p
            ->id = id;
            }

            void AC(node *&root)
            {
                q.push(root);
                
            while(!q.empty())
                {
                    node 
            *= NULL;
                    node 
            *= q.front();
                    q.pop();
                    
            for(int i = 0; i < KIND; i++)
                    {
                        
            if(t->next[i])
                        {
                            p 
            = t->fail;
                            
            while(p)
                            {
                                
            if(p->next[i])
                                {
                                    t
            ->next[i]->fail = p->next[i];
                                    
            break;
                                }
                                p 
            = p->fail;
                            }
                            
            if(!p) t->next[i]->fail = root;
                            q.push(t
            ->next[i]);
                       }
                    }
                }
            }

            bool query(node *&root, char *ch, int count)
            {
                g.clear();
                
            int i = 0, t, top = 0, flag = 0;
                node 
            *= root, *tmp;
                
            while(ch[i])
                {
                    t 
            = ch[i] - 31;
                    
            while(!p->next[t] && p != root) p = p->fail;
                    p 
            = p->next[t];
                    
            if(!p) p = root;
                    tmp 
            = p;
                    
            while(tmp != root && tmp->id)
                    {
                        flag 
            = 1;
                        g.push_back(tmp
            ->id);
                        tmp 
            = tmp->fail;
                    }
                    i
            ++;
                }
                
            if(!flag) return false;
                sort(g.begin(), g.end());
                printf(
            "web %d:", count);
                printf(
            " %d", g[0]);
                
            for(int i = 1; i < g.size(); i++)
                {
                    
            if(g[i] != g[i - 1])
                    printf(
            " %d", g[i]);
                }
                printf(
            "\n");
                
            return true;
            }

            int main()
            {
                
            int n, total;
                
            while(~scanf("%d"&n))
                {
                    node 
            *root = new node();
                    total 
            = 0;
                    
            for(int i = 0; i < n; i++)
                    {
                        scanf(
            "%s", ch);
                        insert(root, ch, i 
            + 1);
                    }
                    AC(root);
                    scanf(
            "%d"&n);
                    
            for(int i = 0; i < n; i++)
                    {
                        scanf(
            "%s", ch);
                        
            if(query(root, ch, i + 1)) total++;
                    }
                    printf(
            "total: %d\n", total);
                }
                
            return 0;
            }
            posted on 2011-01-10 00:11 Fucker 閱讀(679) 評論(1)  編輯 收藏 引用 所屬分類: ACM/ICPC字符串
            評論:
            • # re: HDU 2896 病毒侵襲  小尼人00 Posted @ 2011-05-14 13:04
              #include <stdio.h>
              #include <string.h>
              #include <math.h>
              #include <vector>
              #include <iostream>
              #include <set>
              #include <algorithm>
              #include <queue>
              using namespace std;

              #define kind 129
              #define M 10010

              struct node {
              node *fail;
              node *next[kind];
              int id;
              node() { //構造函數
              fail = NULL;
              id = 0;
              memset(next, NULL, sizeof(next));
              }
              };

              char ch[M]; //鍵盤輸入的串
              queue<node *> q;
              set <int> g;
              int n, m;

              void insert(node *root, char *str, int num) {
              node *p = root;
              int i = 0, index;
              while(str[i]) {
              index = str[i] - 'a';
              if(p->next[index] == NULL) {
              p->next[index] = new node();
              }
              p = p->next[index];
              i++;
              }
              p->id = num;
              }

              void AC (node *&root) { ///root -> fail = NULL;
              q.push(root);
              while(!q.empty()) {
              node *p = NULL;
              node *t = q.front();
              q.pop();
              for(int i = 0; i < kind; i++) {

              if(t->next[i]) {

              p = t->fail;

              while(p) {
              if(p->next[i]) {
              t->next[i]->fail = p->next[i];
              break;
              }
              p = p->fail;
              }
              if(!p) t->next[i]->fail = NULL;
              q.push(t->next[i]);
              }
              }
              }
              }

              bool query(node *&root, char *str, int ca) {
              g.clear();
              int i = 0, t, flag = 0;
              node *p = root, *tmp;
              while(str[i]) {
              t = str[i] - 'a';
              while(!p->next[t] && p != root) { p = p->fail; }
              ///*****************************///
              p = p->next[t];
              ///******************************///
              if(!p) p = root;
              tmp = p;
              while(tmp != root && tmp->id) {
              flag = 1;
              g.insert(tmp->id);
              tmp = tmp->fail;
              }
              i++;
              }
              if(!flag) return false;
              printf("web %d:", ca);
              for(set<int>::iterator it = g.begin(); it != g.end(); it++) {
              cout << ' ' << *it;
              }
              cout << endl;
              return true;
              }

              int main() {
              int total;
              while(~scanf("%d", &n)) {
              node *root = new node();
              total = 0;
              for(int i = 0; i < n; i++) {
              scanf("%s", ch);
              insert(root, ch, i+1);
              }
              AC(root);
              cin >> n;
              for(int i = 0; i < n; i++) {
              cin >> ch;
              if(query(root, ch, i+1)); total++;
              }
              printf("total: %d\n", total);
              }
              return 0;
              }








              幫忙看看吧!樓主,照你的寫的,但是還是不行,輸入都不行  回復  更多評論   

             
            Copyright © Fucker Powered by: 博客園 模板提供:滬江博客
            好久久免费视频高清| 久久人人爽人人精品视频| 一本色道久久HEZYO无码| 久久久久久夜精品精品免费啦| 精品久久久久久无码中文字幕一区| 中文精品久久久久人妻| 久久精品国产亚洲av麻豆色欲| 久久综合综合久久综合| 国产精品伦理久久久久久| 久久久精品人妻一区二区三区蜜桃| 国产欧美久久久精品影院| 国产精品美女久久久m| 久久www免费人成看国产片| 亚洲va久久久噜噜噜久久男同 | 天天躁日日躁狠狠久久| 免费观看久久精彩视频| 欧美精品乱码99久久蜜桃| 国产亚洲精午夜久久久久久| 99蜜桃臀久久久欧美精品网站| 久久影院综合精品| 久久成人国产精品二三区| 亚洲欧洲精品成人久久奇米网| 久久99热这里只频精品6| 香港aa三级久久三级| 国内精品九九久久精品| 欧美久久天天综合香蕉伊| 久久九九亚洲精品| 97久久天天综合色天天综合色hd | 亚洲AV无码久久精品狠狠爱浪潮 | 久久婷婷国产综合精品| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 色综合久久综精品| 亚洲伊人久久精品影院| 久久中文字幕无码专区| 亚洲欧美精品伊人久久| 国产99久久久久久免费看 | 奇米影视7777久久精品人人爽| 久久精品黄AA片一区二区三区| 国产高清国内精品福利99久久| 久久国产欧美日韩精品| 精品久久久久久久久免费影院|