HDU 2222 Keywords Search [AC自动机]
来源:互联网 发布:淘宝店客服怎么设置 编辑:程序博客网 时间:2024/06/08 01:02
http://acm.hdu.edu.cn/showproblem.php?pid=2222
题意:给一堆模式串和一个文本串,问有多少个模式串在文本串中出现过。
AC自动机,在统计答案的时候记得将end清零就好
#include<queue>#include<cassert>#include<algorithm>#include<cmath>#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<set>#include<queue>#include<map>using namespace std;#define rep(i,f,t) for(int i = (f), _end = (t); i <= _end; ++i)#define dep(i,f,t) for(int i = (f), _end = (t); i >= _end; --i)#define clr(c,x) memset(c,x,sizeof(c));#define debug(x) cout<<"debug "<<x<<endl;const int INF = 0x3f3f3f3f;typedef long long int64;inline int RD(){ int res; scanf("%d",&res); return res; }#define Rush for(int casn = RD(), cas = 1; cas <= casn; ++cas)//*******************************************************************************const int maxn = 10005;struct Trie{ int next[maxn*50][26]; int fail[maxn*50]; int end[maxn*50]; int sz; void init(){ sz = 0; clr(next[0],0); } int newnode(){ ++sz; clr(next[sz],0); fail[sz] = end[sz] = 0; return sz; } void insert(char *s){ int u = 0; while(*s){ int nid = *s++ - 'a'; if(!next[u][nid]){ next[u][nid] = newnode(); } u = next[u][nid]; } ++end[u]; } void build(){ queue<int> Q; int u = 0; rep(c,0,25){ if(next[0][c]){ Q.push(next[0][c]); } } while(!Q.empty()){ u = Q.front(); Q.pop(); int fu = fail[u]; rep(c,0,25){ int v = next[u][c]; if(v){ Q.push(v); fail[v] = next[fu][c]; }else{ next[u][c] = next[fu][c]; } } } } int query(char *s){ int u = 0; int ans = 0; while(*s){ int nid = *s++ - 'a'; u = next[u][nid]; int k = u; while(k){ if(end[k]){ ans += end[k]; end[k] = 0; } k = fail[k]; } } return ans; }}ac;char str[1000009];int main(){ Rush{ ac.init(); int n; scanf("%d",&n); while(n--){ scanf("%s",str); ac.insert(str); } ac.build(); scanf("%s",str); printf("%d\n",ac.query(str)); } 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自动机)
- Android 深入ViewPager补间动画,实现类京东商城首页广告Banner切换效果
- Spark:Transformation和Action
- js 百度地图标记定位(一)
- UVA11174 J.Stand in a Line (计数+逆元)
- Storytelling in Public Speeches
- HDU 2222 Keywords Search [AC自动机]
- 第八周项目二——Time类中的运算符重载
- 2--黑马程序员---java必备基础知识总结
- 时间日期控件2
- HDU 3951 Coin Game (博弈)
- Java字符编码及其使用详解
- 牛腩新闻发布系统三之系统发布
- android webview里设置cookie
- 面向对象1