AC自动机-HDU3065-简单题
2015-12-11 00:42
218 查看
http://acm.hdu.edu.cn/showproblem.php?pid=3065
需要记录匹配情况的AC自动机,没有清空一些数组导致wa了几发。
需要记录匹配情况的AC自动机,没有清空一些数组导致wa了几发。
/*--------------------------------------------------------------------------------------*/ // Helica's header // Second Editions // 2015.11.7 // #include <algorithm> #include <iostream> #include <cstring> #include <ctype.h> #include <cstdlib> #include <cstdio> #include <vector> #include <string> #include <queue> #include <stack> #include <cmath> #include <set> #include <map> //debug function for a N*M array #define debug_map(N,M,G) printf("\n");for(int i=0;i<(N);i++)\ {for(int j=0;j<(M);j++){\ printf("%d",G[i][j]);}printf("\n");} //debug function for int,float,double,etc. #define debug_var(X) cout<<#X"="<<X<<endl; /*--------------------------------------------------------------------------------------*/ using namespace std; int N,M,T,ans[2000]; char buf[2000100]; map <int,string > m; struct Trie { int next[50010][26],fail[50010],end[50010],index[50010]; int root,L,cnt; int newnode() { for(int i=0;i<26;i++) next[L][i] = -1; end[L++] = 0; return L-1; } void init() { L = 0; cnt = 0; memset(index,0,sizeof index); memset(ans,0,sizeof ans); root = newnode(); } void insert(char *s) { int len = strlen(s); int now = root; for(int i=0;i<len;i++) { if(next[now][s[i]-'A'] == -1) next[now][s[i]-'A'] = newnode(); now = next[now][s[i]-'A']; } end[now]++; index[now] = cnt++; } void build() { queue <int> Q; fail[root] = root; for(int i=0;i<26;i++) if(next[root][i] == -1) next[root][i] = root; else { fail[next[root][i]] = root; Q.push(next[root][i]); } while(!Q.empty()) { int now = Q.front(); Q.pop(); for(int i=0;i<26;i++) { if(next[now][i] == -1) next[now][i] = next[fail[now]][i]; else { fail[next[now][i]] = next[fail[now]][i]; Q.push(next[now][i]); } } } } void query(char *s) { int len = strlen(s); int now = root; for(int i=0;i<len;i++) { if(s[i]<'A'||s[i]>'Z') { now = root; continue; } now = next[now][s[i]-'A']; int temp = now; while(temp != root) { ans[index[temp]] += end[temp]; temp = fail[temp]; } } } void debug() { for(int i=0;i<L;i++) { printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]); for(int j=0;j<26;j++) printf("%2d",next[i][j]); printf("]\n"); } } }ac; int main() { while(~scanf("%d",&N)) { ac.init(); m.clear(); for(int i=0;i<N;i++) { scanf("%s",buf); m.insert(pair<int,string>(i,string(buf))); ac.insert(buf); } getchar(); ac.build(); scanf("%s",buf); ac.query(buf); for(int i=0;i<N;i++) if(ans[i]) { printf("%s: %d\n",m[i].c_str(),ans[i]); } } }
相关文章推荐
- A20_CORE网络问题之硬件问题!!!
- 蓝桥杯 新建Microsoft Word文档(数组)
- 对对象的理解
- 64位WINDOWS系统环境下应用软件开发的兼容性问题(CPU 注册表 目录)
- hdu 4628 状态压缩dp
- 第3章 数据缓冲区高速缓冲
- AC自动机-HDU2896-模板题
- mysql_query设置字符集为utf8,解决中文乱码的问题
- Java学习----对象的状态和行为
- spring定时器在集群环境下的执行
- 通过注册表修改键盘按键的映射
- AC自动机-HDU2222-模板题
- oracle存储过程的学习
- unity,set ugui rectTransform anchor by script
- canfestival中对象字典编辑器的打开
- 分布式版本控制系统----GIT的使用(一)
- Maridb编译安装及存储引擎、事务
- 传统企业信息化的成功案例的一些感想(原创)
- DBA推荐使用 HeidiSQL,开源免费,不用担心有病毒
- MySql