模板(字符串)
来源:互联网 发布:不会英语怎么学编程 编辑:程序博客网 时间:2024/06/10 01:06
ac自动机
//UVALive 4670#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <queue>#include <map>#include <string>using namespace std;const int MAXN = 21000;const int MAXM = 1000001;const int INF = 1 << 30;const int SEGMA_SZ = 26;int ch[MAXN][SEGMA_SZ],cnt;int pre[MAXN],isWord[MAXN],lastWord[MAXN];int calc[MAXN],n,maxTime;char s[MAXM];map<int, string> hash;void init(){ memset(ch[0], 0, sizeof(ch[0])); cnt=0; memset(calc, 0 ,sizeof(calc)); hash.clear(); maxTime=0;}void Insert(char s[],int id){ int u=0; for (int i = 0; s[i]; ++i){ int v = s[i] - 'a'; if (!ch[u][v]){ cnt++; memset(ch[cnt], 0, sizeof(ch[cnt])); isWord[cnt] = lastWord[cnt] = 0; ch[u][v] = cnt; } u = ch[u][v]; } isWord[u] = id;}queue<int> q;void getPre(){ while (!q.empty()) q.pop(); pre[0]=0; for (int i = 0; i < SEGMA_SZ; ++i) if (ch[0][i]){ int v = ch[0][i]; pre[v] = 0; q.push(v); } while (!q.empty()){ int u = q.front(); q.pop(); for (int i = 0; i < SEGMA_SZ; ++i){ int v = ch[u][i]; if (!v) continue; q.push(v); int tmp_pre = pre[u]; while (tmp_pre && !ch[tmp_pre][i]) tmp_pre = pre[tmp_pre]; pre[v]=ch[tmp_pre][i]; lastWord[v] = isWord[pre[v]] ? pre[v] : lastWord[pre[v]]; } }}void Find(char s[]){ int j = 0; for (int i = 0; s[i]; ++i){ int v = s[i] - 'a'; while (j && !ch[j][v]) j = pre[j]; j = ch[j][v]; int tmp_pre=j; do{ calc[isWord[tmp_pre]]++; tmp_pre = lastWord[tmp_pre]; if (!tmp_pre) break; }while (1); } for (int i = 1; i <= n; ++i) maxTime = max(maxTime, calc[i]);}int main(){ while (~scanf("%d", &n) && n ){ init(); for (int i = 1; i <= n; ++i){ scanf("%s", s); hash[i]=string(s); calc[i]=0; Insert(s, i); } scanf("%s", s); getPre(); Find(s); printf("%d\n", maxTime); for(int i = 1; i <= n; ++i) if (calc[i] == maxTime) cout << hash[i] << endl; } return 0;}
0 0
- 模板(字符串)
- 字符串匹配 KMP(模板)
- 模板库(字符串相关)
- 字符串模板
- 字符串模板
- 模板字符串
- 字符串模板
- 字符串模板
- 模板字符串
- 字符串与字符串模板
- 字符串模板总结(一):AC自动机
- 字符串模板总结(三):KMP
- 字符串模板总结(四):后缀数组
- 字符串模板总结(五):后缀自动机
- poj 3461(kmp字符串匹配模板)
- 深入浅出ES6(四):模板字符串
- 字符串问题 ---模板(不定期更新中。)
- 深入浅出ES6(四):模板字符串
- 两年前实习时的文档——Platform学习总结
- 使用Timer实现延迟welcome界面2秒后启动主activity
- OpenCV2马拉松第7圈——图像金字塔
- AccessPort 0d 0a乱码
- linux网络编程
- 模板(字符串)
- 结构体字节对齐
- JS实现高级查询——网页
- 麻痹的,浪费了2个月的时间,终于明白了,android网站上的help是最好的
- Netty中文用户手册(二)
- poj 2236 Wireless Network 并查集
- 2014-05-01 Arm架构的处理器模式和寄存器用途
- 7.8 VC2010下Qt5的中文乱码问题
- POJ 1434 Fill the Cisterns!(二分)