码迷,mamicode.com
首页 > 其他好文 > 详细

杭电 2222 Keywords Search

时间:2018-08-17 20:33:51      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:new   using   empty   sample   cst   php   character   技术分享   lse   

题目连接:点我
Problem Description
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.

Input
First line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters ‘a’-‘z’, and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.

Output
Print how many keywords are contained in the description.

Sample Input
1
5
she
he
say
shr
her
yasherhs

Sample Output
3

ac自动机裸题

技术分享图片
  1 #include<queue>
  2 #include<set>
  3 #include<cstdio>
  4 #include <iostream>
  5 #include<algorithm>
  6 #include<cstring>
  7 #include<cmath>
  8 using namespace std;
  9 char c[1000000+10];
 10 struct acnode{
 11     int sum;
 12     acnode* next[26];
 13     acnode* fail;
 14     acnode(){
 15         for(int i =0;i<26;i++)
 16             next[i]=NULL;
 17         fail= NULL;
 18         sum=0;
 19     }
 20 };
 21 acnode *root;
 22 int cnt;
 23 acnode* newnode(){
 24     acnode *p = new acnode;
 25     for(int i =0;i<26;i++)
 26         p->next[i]=NULL;
 27     p->fail = NULL;
 28     p->sum=0;
 29     return p;
 30 }
 31 //插入函数
 32 void Insert(char *s)
 33 {
 34     acnode *p = root;
 35     for(int i = 0; s[i]; i++)
 36     {
 37         int x = s[i] - a;
 38         if(p->next[x]==NULL)
 39         {
 40             acnode *nn=newnode();
 41             for(int j=0;j<26;j++)
 42                 nn->next[j] = NULL;
 43             nn->sum = 0;
 44             nn->fail = NULL;
 45             p->next[x]=nn;
 46         }
 47         p = p->next[x];
 48     }
 49     p->sum++;
 50 }
 51 //获取fail指针,在插入结束之后使用
 52 void getfail(){
 53     queue<acnode*> q;
 54     for(int i = 0 ; i < 26 ; i ++ )
 55     {
 56         if(root->next[i]!=NULL){
 57             root->next[i]->fail = root;
 58             q.push(root->next[i]);
 59         }
 60     }
 61     while(!q.empty()){
 62         acnode* tem = q.front();
 63         q.pop();
 64         for(int i = 0;i<26;i++){
 65             if(tem->next[i]!=NULL)
 66             {
 67                 acnode *p;
 68                 if(tem == root){
 69                     tem->next[i]->fail = root;
 70                 }
 71                 else
 72                 {
 73                     p = tem->fail;
 74                     while(p!=NULL){
 75                         if(p->next[i]!=NULL){
 76                             tem->next[i]->fail = p->next[i];
 77                             break;
 78                         }
 79                         p=p->fail;
 80                     }
 81                     if(p==NULL)
 82                         tem->next[i]->fail = root;
 83                 }
 84                 q.push(tem->next[i]);
 85             }
 86         }
 87     }
 88 }
 89 //匹配函数
 90 void ac_automation(char *ch)
 91 {
 92     acnode *p = root;
 93     int len = strlen(ch);
 94     for(int i = 0; i < len; i++)
 95     {
 96         int x = ch[i] - a;
 97         while(p->next[x]==NULL && p != root)//没匹配到,那么就找fail指针。
 98             p = p->fail;
 99         p = p->next[x];
100         if(!p)
101             p = root;
102         acnode *temp = p;
103         while(temp != root)
104         {
105            if(temp->sum >= 0)
106             /*
107             在这里已经匹配成功了,执行想执行的操作即可,怎么改看题目需求+
108             */
109            {
110                cnt += temp->sum;
111                temp->sum = -1;
112            }
113            else break;
114            temp = temp->fail;
115         }
116     }
117 }
118 
119 int main()
120 {   int t;
121     scanf("%d",&t);
122     while(t--){
123         cnt = 0;
124     int n;
125     cin>>n;
126 
127     root = newnode();
128     for(int i = 0 ;i < n;i++){
129         scanf("%s",c);
130         Insert(c);
131     }
132     getfail();
133     //int m ;
134     //cin>> m;
135     //for(int i = 0;i<m;i++){
136         scanf("%s",c);
137         ac_automation(c);
138     //}
139     cout<<cnt<<endl;
140     } 
141 
142     return 0;
143 }
ac自动机

 

杭电 2222 Keywords Search

标签:new   using   empty   sample   cst   php   character   技术分享   lse   

原文地址:https://www.cnblogs.com/tongxiong/p/9495121.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!