hdu2222-- Keywords Search(AC自动机入门1)
来源:互联网 发布:mac战网四合一 编辑:程序博客网 时间:2024/06/02 06:54
Keywords Search
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Appoint description:
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.
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.
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
15shehesayshrheryasherhs
Sample Output
3
入门的问题,给出n个字符串,问结果字符串可以匹配到几个给出的字符串。
自动机的模板题,留作模板了
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std ;struct node{ int flag ; node *next[26] , *fail ;};queue <node*> que ;char str[1100000] , s[100] ;node *newnode(){ node *p ; p = new node ; p->flag = 0 ; p->fail = NULL ; for(int i = 0 ; i < 26 ; i++) p->next[i] = NULL ; return p ;}void settree(char *s,node *p){ int i , k , l = strlen(s) ; for(i = 0 ; i < l ; i++) { k = s[i] - 'a' ; if( p->next[k] == NULL ) p->next[k] = newnode() ; p = p->next[k] ; } p->flag++ ; return ;}void setfail(node *rt){ int i ; node *p , *q ; while( !que.empty() ) que.pop() ; que.push(rt) ; while( !que.empty() ) { p = que.front() ; que.pop() ; for(i = 0 ; i < 26 ; i++) { if( p->next[i] ) { q = p->fail ; while( q && !q->next[i] ) q = q->fail ; p->next[i]->fail = q == NULL ? rt : q->next[i] ; que.push(p->next[i]) ; } else p->next[i] = p == rt ? rt : p->fail->next[i] ; } }}int query(char *str,node *rt){ int num = 0 , i , k , l = strlen(str) ; node *p = rt , *temp ; for(i = 0 ; i < l ; i++) { k = str[i] - 'a' ; p = p->next[k] ; temp = p ; while( temp && temp->flag ) { num += temp->flag ; temp->flag = 0 ; temp = temp->fail ; } } return num ;}int main(){ int t , n , i ; node *rt ; scanf("%d", &t) ; while( t-- ) { rt = newnode() ; scanf("%d", &n) ; while( n-- ) { scanf("%s", s) ; settree(s,rt) ; } setfail(rt) ; scanf("%s", str) ; printf("%d\n", query(str,rt)) ; } return 0;}
1 0
- hdu2222-- Keywords Search(AC自动机入门1)
- HDU2222 Keywords Search AC自动机入门
- HDU2222 Keywords Search(AC自动机入门)
- [AC自动机]hdu2222 Keywords Search
- HDU2222 Keywords Search AC自动机
- 【hdu2222】【AC自动机】Keywords Search
- HDU2222 Keywords Search AC自动机
- HDU2222 Keywords Search AC自动机
- [HDU2222] Keywords Search && AC自动机
- HDU2222 Keywords Search AC自动机
- HDU2222 Keywords Search【AC自动机】
- AC自动机 - hdu2222 Keywords Search
- HDU2222-Keywords Search AC自动机
- AC自动机 hdu2222 Keywords Search
- 【AC自动机】[HDU2222]Keywords Search
- HDU2222: Keywords Search AC自动机
- 【AC自动机】Hdu2222:Keywords Search
- HDU2222 Keywords Search [AC自动机]
- Loading Large Bitmaps Efficiently
- Lua的function、closure和upvalue
- [Leetcode]Unique Binary Search Trees II
- JS中如何判断null、undefined与NaN
- jquery on()方法
- hdu2222-- Keywords Search(AC自动机入门1)
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) E. Pretty Song
- WinIo驱动级键盘模拟编程
- 国外一些有价值的docker相关的文章
- 修改WordPress主题导致整个站点404无法访问
- 在做iframe自适应的时候,iframe高度不断自增长
- Masonry介绍与使用实践:快速上手Autolayout
- web 跨域请求共享资源(OCRS)
- 商场促销-策略模式