ac自动机模板hdu2222
来源:互联网 发布:喜马拉雅字体mac版 编辑:程序博客网 时间:2024/06/05 02:10
给你一堆字符串,和一个文本串,求有多少个字符串在文本串出现过
#include<cstdio>#include<iostream>#include<cstring>#include<queue>using namespace std;const int maxn=550000;struct AC_auto{ int chd[maxn][26],v[maxn],f[maxn],last[maxn],sz,ans; void init() { sz=1;ans=0; memset(v,0,sizeof(v)); memset(f,0,sizeof(f)); memset(chd[0],0,sizeof(chd[0])); } void insert(char* p) { int cur=0; for(;*p;p++) { if(!chd[cur][*p-'a']) { memset(chd[sz],0,sizeof(chd[sz])); chd[cur][*p-'a']=sz++; } cur=chd[cur][*p-'a']; } v[cur]++; } bool query(char* p) { int cur=0; for(;*p;p++) { if(!chd[cur][*p-'a']) break; cur=chd[cur][*p-'a']; } return v[cur]&&(!(*p)); } int getFail() { queue<int> q; f[0]=0; for(int c=0;c<26;c++) { int u=chd[0][c]; if(u) { f[u]=0; q.push(u); last[u]=0; } } while(!q.empty()) { int r=q.front(); q.pop(); for(int c=0;c<26;c++) { int u=chd[r][c]; if(!u){ chd[r][c]=chd[f[r]][c];continue;}///.... q.push(u); int vv=f[r]; while(vv&&!chd[vv][c]) vv=f[vv]; f[u]=chd[vv][c]; last[u]=v[f[u]] ? f[u] : last[f[u]]; } } } void solve(int j) { if(!j) return; if(v[j]) { ans+=v[j]; v[j]=0; } solve(last[j]); } void find(char* T) { int n=strlen(T),j=0; getFail(); for(int i=0;i<n;i++) { j=chd[j][T[i]-'a']; if(v[j]) solve(j); else if(last[j]) solve(last[j]); } }}ac;int main(){int T,n;char s[1000005];cin>>T;while(T--){scanf("%d",&n);ac.init();while(n--){scanf("%s",s);ac.insert(s);}scanf("%s",s);ac.find(s);printf("%d\n",ac.ans);}return 0;}
hdu2222
阅读全文
1 0
- AC自动机模板 hdu2222
- hdu2222 ac自动机模板
- HDU2222 AC自动机模板
- AC自动机模板 hdu2222
- hdu2222 ac自动机模板
- ac自动机模板hdu2222
- hdu2222 ac自动机模板题
- AC自动机模板(hdu2222)
- ac自动机模板(hdu2222)
- ac自动机模板(hdu2222)
- HDU2222 AC自动机 入门模板
- hdu2222 AC自动机入门 指针型模板
- AC自动机模板(数组+指针)hdu2222
- HDU2222 Keywords Search AC自动机模板题
- HDU2222 Keywords Search(AC自动机模板题)
- hdu2222--Keywords Search+AC自动机模板
- hdu2222&hdu3065 AC自动机模板题
- HDU2222 Keywords Search [AC自动机模板]
- STL容器vector内存的正确释放
- centos7 无法启动网络(service network restart)错误解决办法
- ZOJ 1002 Fire Net
- Git下使用Beyond Compare作为比较和合并工具
- 三大面试排序的归并以及递归实现--快速排序、归并排序、堆排序
- ac自动机模板hdu2222
- 方便自己查对蓝牙设备类型 Class of Device (CoD) Calculator
- PAT1116. Come on! Let's C (map)
- 音乐
- hive 教程
- 上传本地项目到github
- struts2模型驱动深入理解
- [NOIP2016提高组]D2T1:组合数问题
- Mat数据结构