HDU - 2222 Keywords Search AC自动机
来源:互联网 发布:淘宝3000多的实体娃娃 编辑:程序博客网 时间:2024/05/20 01:10
Keywords Search
HDU - 2222In 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.
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.
15shehesayshrheryasherhs
3
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <queue>#include <map>using namespace std;typedef long long LL;const int CHAR_SIZE = 26;const int MAX_SIZE = 5e5 + 8;map<char, int> mp;struct AC_Machine{ int ch[MAX_SIZE][CHAR_SIZE], danger[MAX_SIZE], fail[MAX_SIZE]; int sz; inline void init(){ sz = 1; memset(ch[0], 0, sizeof ch[0]); memset(danger, 0, sizeof danger); } inline void _insert(const string &s){ int n = s.size(); int u = 0, c; for(int i = 0; i < n; i++){ c = mp[s[i]]; if(!ch[u][c]){ memset(ch[sz], 0, sizeof ch[sz]); danger[sz] = 0; ch[u][c] = sz++; } u = ch[u][c]; } danger[u]++; } inline void _build(){ queue<int> Q; fail[0] = 0; for(int c = 0, u; c < CHAR_SIZE; c++){ u = ch[0][c]; if(u){Q.push(u); fail[u] = 0;} } int r; while(!Q.empty()){ r = Q.front(); Q.pop(); //danger[r] |= danger[fail[r]]; for(int c = 0, u; c < CHAR_SIZE; c++){ u = ch[r][c]; if(!u){ch[r][c] = ch[fail[r]][c]; continue; } fail[u] = ch[fail[r]][c]; Q.push(u); } } }}ac;string s;int main(){ #ifdef LOCAL freopen("3.in", "r", stdin); //freopen("3.out", "w", stdout); #endif // LOCAL ios::sync_with_stdio(false); cin.tie(0); int T, n, len, now, i, ans, tmp; for(int i = 0; i < 26; i++){mp[(i + 'a')] = i;} cin >> T; while(T--){ cin >> n; ac.init(); while(n--){ cin >> s; ac._insert(s); } ac._build(); cin >> s; ans = 0; len = s.size(); now = 0; for(i = 0; i < len; i++){ now = ac.ch[now][mp[s[i]]]; tmp = now; while(tmp){ if(ac.danger[tmp]){ ans += ac.danger[tmp]; ac.danger[tmp] = 0; } tmp = ac.fail[tmp]; } } cout << ans << "\n"; } return 0;}
Thank you!
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自动机)
- LightOJ 1106 Gone Fishing 贪心, DP, 堆优化 2017年1月23日
- 笔记6
- 1031.Hello World for U (20)
- BUPT OJ 非平方等式
- noi-1703-与7无关的数
- HDU - 2222 Keywords Search AC自动机
- 机器学习面试准备(持续更新)--- 优秀博文传送门,收集优秀资源
- 可拆分物品背包问题
- SQL 不常用的一些命令sp_OACreate,xp_cmdshell,sp_makewebtask
- ifstream类使用心得一则
- 第三方视频播放器
- 欢迎使用CSDN-markdown编辑器
- bzoj 1051 缩点重构图
- 寒假训练赛(四) 自我总结