spoj 1812 Longest Common Substring II(后缀自动机)
2015-11-09 11:20
344 查看
题目链接:spoj 1812 Longest Common Substring II
代码
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int maxn = 100005; const int SIGMA_SIZE = 26; const int inf = 0x3f3f3f3f; struct SAM { int sz, last; int g[maxn<<1][SIGMA_SIZE], pre[maxn<<1], step[maxn<<1]; int lcs[maxn<<1], tlcs[maxn<<1]; void newNode(int s) { step[++sz] = s; pre[sz] = 0; lcs[sz] = s; memset(g[sz], 0, sizeof(g[sz])); } int idx(char ch) { return ch -'a'; } void init() { sz = 0, last = 1; newNode(0); memset(tlcs, 0, sizeof(tlcs)); } void insert(char ch) { newNode(step[last] + 1); int v = idx(ch), p = last, np = sz; while (p && !g[p][v]) { g[p][v] = np; p = pre[p]; } if (p) { int q = g[p][v]; if (step[q] == step[p] + 1) pre[np] = q; else { newNode(step[p] + 1); int nq = sz; for (int j = 0; j < SIGMA_SIZE; j++) g[nq][j] = g[q][j]; pre[nq] = pre[q]; pre[np] = pre[q] = nq; while (p && g[p][v] == q) { g[p][v] = nq; p = pre[p]; } } } else pre[np] = 1; last = np; } void match(char* str) { memset(tlcs, 0 , sizeof(tlcs)); int n = strlen(str), p = 1, ans = 0; for (int i = 0; i < n; i++) { int v = idx(str[i]); if (g[p][v]) p = g[p][v], ans++; else { while (p && g[p][v] == 0) p = pre[p]; if (p) ans = step[p] + 1, p = g[p][v]; else ans = 0, p = 1; } tlcs[p] = max(tlcs[p], ans); } for (int i = 0; i < sz; i++) { int u = id[i]; lcs[u] = min(lcs[u], tlcs[u]); int v = pre[u]; //printf("%d %d %d!", u, v, tlcs[u]); tlcs[v] = max(tlcs[v], tlcs[u]); } //printf("\n"); } int solve() { int ans = 0; for (int i = 1; i <= sz; i++) { //printf("%d ", lcs[i]); ans = max(ans, lcs[i]); } //printf("\n"); return ans; } int du[maxn<<1], id[maxn<<1]; void topu() { memset(du, 0, sizeof(du)); for (int i = 1; i <= sz; i++) du[pre[i]]++; int head = 0, rear = 0; for (int i = 1; i <= sz; i++) if (du[i] == 0) id[rear++] = i; while (head < rear) { int u = id[head++]; du[pre[u]]--; if (du[pre[u]] == 0 && pre[u]) id[rear++] = pre[u]; } } }SA; char str[20][maxn]; int main () { int N = 0; while (gets(str ) != NULL) N++; SA.init(); int len = strlen(str[0]); for (int i = 0; i < len; i++) SA.insert(str[0][i]); SA.topu(); for (int i = 1; i < N; i++) SA.match(str[i]); printf("%d\n", SA.solve()); return 0; }
相关文章推荐
- 在vc中实现获取汉字拼音的首字母
- 将数据封装为Json数据格式
- OSI模型的理解
- CSS 之 Opacity多浏览器透明度兼容处理
- 使用[NSKeyedUnarchiver unarchiveObjectWithData:data]程序crash
- HG,GIT,SVN版本控制系统
- 下拉框编辑模式设置
- RSA算法原理
- 嗅探回环地址数据的工具RawCap
- 桑基图(sankey)的绘制
- Mysql 定时任务 事件处理存储过程详解
- How do I develop a service?
- spoj 1811 Longest Common Substring(后缀自动机)
- log4net应用实践(一)
- 技术一览表
- 使用Qt开发中国象棋(一):概述
- AndroidUI设计——Gridview
- 邻接表保存图
- window.location.hash知识汇总
- [LeetCode] Valid Anagram