[SPOJ1812]LCS2 - Longest Common Substring II
来源:互联网 发布:iface102下载数据 编辑:程序博客网 时间:2024/05/22 16:05
LCS2 - Longest Common Substring II
A string is finite sequence of characters over a non-empty finite set Σ.
In this problem, Σ is the set of lowercase letters.
Substring, also called factor, is a consecutive sequence of characters occurrences at least once in a string.
Now your task is a bit harder, for some given strings, find the length of the longest common substring of them.
Here common substring means a substring of two or more strings.
Input
The input contains at most 10 lines, each line consists of no more than 100000 lowercase letters, representing a string.
Output
The length of the longest common substring. If such string doesn’t exist, print “0” instead.
Example
Input:
alsdfkjfjkdsal
fdjskalajfkdsla
aaaajfaaaa
Output:
2
Solution
把一个串建成自动机,其它的在上面跑,统计出每个串在每个状态的最大匹配长度,每个状态取所有串的最小值得到所有串在该状态的最大匹配长度,最后扫描所有状态取最大值得到结果
Code
#include <bits/stdc++.h>using namespace std;#define rep(i, l, r) for (int i = (l); i <= (r); i++)#define per(i, r, l) for (int i = (r); i >= (l); i--)#define MS(_) memset(_, 0, sizeof(_))#define MP make_pair#define PB push_backinline void ckmax(int &x, int y){ if (x < y) x = y; }inline void ckmin(int &x, int y){ if (x > y) x = y; }const int INF = 0x7fffffff;const int N = 200100;char s[N];int len[N], nxt[N][26], fa[N], d[N], t[N], f[N], mn[N];struct sam{ int root, last, cnt, LEN; sam() { cnt = 0; root = last = ++cnt; } inline void insert(int c){ int np = ++cnt, p = last; last = np; mn[np] = len[np] = len[p]+1; for (; p && !nxt[p][c]; nxt[p][c] = np, p = fa[p]); if (!p) fa[np] = root; else if (len[nxt[p][c]] == len[p]+1) fa[np] = nxt[p][c]; else{ int nq = ++cnt, q = nxt[p][c]; mn[nq] = len[nq] = len[p]+1; memcpy(nxt[nq], nxt[q], sizeof nxt[q]); fa[nq] = fa[q]; fa[q] = fa[np] = nq; for (; p && nxt[p][c] == q; nxt[p][c] = nq, p = fa[p]); } } inline void build(){ char s[N]; scanf("%s", s+1); LEN = strlen(s+1); rep(i, 1, LEN) insert(s[i]-'a'); } inline void topsort(){ rep(i, 1, cnt) d[len[i]]++; rep(i, 1, LEN) d[i] += d[i-1]; rep(i, 1, cnt) t[d[len[i]]--] = i; } inline bool run(){ if (scanf("%s", s+1) == EOF) return false; MS(f); int _len = strlen(s+1), nowlen = 0; for (int i = 1, p = root; i <= _len; i++){ int c = s[i]-'a'; if (nxt[p][c]) { nowlen++; p = nxt[p][c]; } else{ for (; p && !nxt[p][c]; p = fa[p]); if (!p) { p = root; nowlen = 0; } else { nowlen = len[p] + 1; p = nxt[p][c]; } } ckmax(f[p], nowlen); } per(i, cnt, 1){ int now = t[i]; ckmin(mn[now], f[now]); //if (f[now]&&fa[now]) f[fa[now]] = len[fa[now]]; ckmax(f[fa[now]], f[now]); } return true; }}SAM;int main(){ SAM.build(); SAM.topsort(); while (SAM.run()); int ans = 0; rep(i, 1, SAM.cnt) ckmax(ans, mn[i]); printf("%d\n", ans); return 0;}
- [SPOJ1812]LCS2 - Longest Common Substring II
- spoj1812 Longest Common Substring II(LCS2),后缀自动机
- [SPOJ1812]LCS2 - Longest Common Substring II(后缀自动机)
- spoj1812 Longest Common Substring II(LCS2)多个字符串
- 【spoj1812】Longest Common Substring II 【SAM】
- [spoj1812]Longest Common Substring II && 后缀自动机
- 【spoj1812】Longest Common Substring II 后缀自动机
- SPOJ1812 Longest Common Substring II 后缀自动机
- SPOJ LCS2 1812. Longest Common Substring II
- SPOJ LCS2 Longest Common Substring II
- SPOJ LCS2 Longest Common Substring II
- SPOJ LCS2 Longest Common Substring II
- SPOJ LCS2 Longest Common Substring II
- BZOJ2946 SPOJ1812:Longest Common Substring II(后缀自动机)
- SPOJ LCS2 Longest Common Substring II 后缀自动机
- 【后缀自动机】[SPOJ LCS2]Longest Common Substring II
- spoj 1812 LCS2 - Longest Common Substring II (后缀自动机)
- SPOJ lcs2 Longest Common Substring II 后缀自动机
- 树的直径
- hibernate4 映射集合属性Map
- JVM系列:四、Java类加载机制总结
- LeetCodet题解--16. 3Sum Closest(离给定值最近的三数之和)
- 求任意两个点之间的最短路径
- [SPOJ1812]LCS2 - Longest Common Substring II
- hdu 5294 最短路+最大流
- 源码--文件上传
- 蛇形巨阵
- Xcode 7 Unknow type name
- Java序列化与反序列化
- Spring和mybatis整合时报出的错误
- stm32上NRF24L01无法通信的问题。。
- Hilbert R树发展