【AC自动机】HDU-2222 Keywords Search
来源:互联网 发布:淘宝宝贝关键词优化 编辑:程序博客网 时间:2024/05/16 07:09
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
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.
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
————————————————————————————————————————————————————————
思路:AC自动机的模板题,用来测试模板。
P.S. 本题很坑的地方是这两组数据:
2
6
she
he
he
say
shr
her
yasherhs
she
he
he
say
shr
her
yasherhs
Output: 4
6
she
he
he
say
shr
her
yasherhe
6
she
he
he
say
shr
her
yasherhe
Output: 4
代码如下:
#include <cstdio>#include <cstdlib>#include <cstring>#include <queue>using namespace std;const int N = 5e5+5, M = 1e6+5;struct Trie { Trie *next[26]; Trie *fail; int val;}tree[N];queue <Trie *> q;int idx[128];char txt[M];class ac_auto { private: int nxt; Trie *root; public: ac_auto() { nxt = 0; root = add(); } Trie *add() { memset(&tree[nxt], 0, sizeof(Trie)); return &tree[nxt++]; } void insert(char *s) { Trie *rt = root; int len = strlen(s); for(int i = 0; i < len; i++) { int c = idx[s[i]+0]; if(!rt->next[c]) rt->next[c] = add(); rt = rt->next[c]; } rt->val++; } void get_f() { queue <Trie *> q; q.push(root); root->fail = NULL; while(!q.empty()) { Trie *u = q.front(); q.pop(); for(int c = 0; c < 26; c++) { if(u->next[c]) { Trie *f = u->fail; while(f) { if(f->next[c]) { u->next[c]->fail = f->next[c]; break; } f = f->fail; } if(!f) u->next[c]->fail = root; q.push(u->next[c]); } } } } int match(char *s) { Trie *rt = root; int len = strlen(s), ret = 0; for(int i = 0; i < len; i++) { int c = idx[s[i]+0]; while(!rt->next[c] && rt != root) rt = rt->fail; rt = rt->next[c]; if(!rt) rt = root; Trie *p = rt; while(p != root) { if(p->val) { ret += p->val; p->val = 0; } else break; p = p->fail; } } return ret; }};void haxi(){ for(int i = 0; i < 26; i++) idx['a'+i] = i;}int main(){#ifdef J_Sure freopen("000.in", "r", stdin); //freopen("999.out", "w", stdout);#endif int T; scanf("%d", &T); haxi(); while(T--) { ac_auto ac; int m; char str[55]; scanf("%d", &m); while(m--) { scanf("%s", str); ac.insert(str); } ac.get_f(); scanf("%s", txt); int ans = ac.match(txt); printf("%d\n", ans); } return 0;}
0 0
- HDU 2222 Keywords Search (AC自动机)
- hdu 2222 Keywords Search //ac自动机
- hdu 2222 Keywords Search AC自动机详解
- hdU 2222 Keywords Search(AC自动机)
- hdu 2222 Keywords Search--AC自动机
- hdu 2222 Keywords Search(AC自动机)
- hdu 2222 Keywords Search AC自动机 模板
- hdu 2222 Keywords Search AC自动机 模板
- hdu 2222 Keywords Search(AC自动机)
- HDU 2222 Keywords Search(AC自动机)
- hdu 2222 Keywords Search(AC自动机模板)
- HDU 2222 Keywords Search 【AC自动机模板】
- hdu 2222 Keywords Search AC自动机
- HDU 2222 Keywords Search (AC自动机)
- hdu - 2222 - Keywords Search(AC自动机)
- [HDU 2222]Keywords Search[AC自动机]
- hdu 2222 Keywords Search----AC自动机
- hdu 2222 Keywords Search(AC自动机)
- spring security3.x学习(14)_关系型数据库使用的UserDetailsService(JdbcDaoImpl)
- 九度OJ—题目1351:数组中只出现一次的数字(vector与数组的区别)
- 使用eclipse更新Android SDK Tools 报错 failed to execute tools\android.bat
- 数据库分库分表
- Android中dip、dp、sp、pt和px的区别
- 【AC自动机】HDU-2222 Keywords Search
- MySQL字符编码的讨论:如何处理emoji等4字节的Unicode字符 - utf8mb4 vs. utf8 Collations
- Python中的推导式介绍
- Android权限大全
- Stanford机器学习---第七讲. 机器学习系统设计
- iOS多线程
- ios学习总结的非常好
- Stanford机器学习---第八讲. 支持向量机SVM
- KJFrameForAndroid框架学习----高效加载Bitmap