[BZOJ 2946]公共串
来源:互联网 发布:quickshow软件下载 编辑:程序博客网 时间:2024/05/22 20:26
后缀自动机SAM
249. [POI2000] 最长公共子串
时间限制:1 s 内存限制:64 MB
给出几个由小写字母构成的单词,求它们最长的公共子串的长度。
任务
从文件中读入单词
计算最长公共子串的长度
输出结果到文件
输入
文件的第一行是整数 n,1<=n<=5,表示单词的数量。接下来n行每行一个单词,只由小写字母组成,单词的长度至少为1,最大为2000。
输出:
仅一行,一个整数,最长公共子串的长度。
样例输入:
3
abcb
bca
acbc
样例输出:
2
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define maxn 2100using namespace std;int n;char s[5][maxn];struct Node{int len, nxt[26], link, mx[5];}st[maxn << 1];int root, size, last;void init(){last = root = size = 0;st[root].link = -1;st[root].len = 0;}void Extend(char ch){int cur = ++ size, c = ch - 'a', p;st[cur].len = st[last].len + 1;for(p = last; ~p && !st[p].nxt[c]; p = st[p].link) st[p].nxt[c] = cur;if(p == -1) st[cur].link = root;else{int q = st[p].nxt[c];if(st[p].len + 1 == st[q].len) st[cur].link = q;else{int clone = ++ size;st[clone] = st[q];st[clone].len = st[p].len + 1;for(; p != -1 && st[p].nxt[c] == q; p = st[p].link) st[p].nxt[c] = clone;st[cur].link = st[q].link = clone;}}last = cur;}int main(){#ifndef ONLINE_JUDGEfreopen("pow.in","r",stdin);freopen("pow.out","w",stdout);#endifscanf("%d", &n);for(int i = 0; i < n; i ++) scanf("%s", s[i] + 1);int m = strlen(s[0] + 1);init();for(int i = 1; i <= m; i ++) Extend(s[0][i]);for(int i = 1; i < n; i ++){int step = 0;int N = strlen(s[i] + 1), now = root;for(int j = 1; j <= N; j ++){int c = s[i][j] - 'a';if(st[now].nxt[c])now = st[now].nxt[c], step ++;else{for(; ~now && st[now].nxt[c] == 0; now = st[now].link);if(now == -1)now = root, step = 0;else step = st[now].len + 1, now = st[now].nxt[c];//here!}st[now].mx[i] = max(st[now].mx[i], step);}}static int w[maxn << 1], t[maxn << 1];memset(w, 0, sizeof w);for(int i = 1; i <= size; i ++) w[st[i].len] ++;for(int i = 2; i <= size; i ++) w[i] += w[i - 1];for(int i = size; i >= 1; i --) t[w[st[i].len] --] = i;for(int i = size; i >= 1; i --){for(int j = 1; j < n; j ++){int &ans = st[st[t[i]].link].mx[j];ans = max(ans, st[t[i]].mx[j]);}}int ans = 0;for(int i = 1; i <= size; i ++){int mn = st[i].len;for(int j = 1; j < n; j ++) mn = min(mn, st[i].mx[j]);ans = max(ans, mn);}printf("%d\n", ans);return 0;}
0 0
- [BZOJ 2946]公共串
- bzoj 2946: [Poi2000]公共串
- BZOJ 2946 [Poi2000]公共串
- BZOJ 2946 Poi2000 公共串 后缀自动机
- BZOJ 2946 [Poi2000]公共串 后缀数组
- 【BZOJ 2946】[Poi2000]公共串 后缀数组
- bzoj 2946: [Poi2000]公共串 后缀自动机
- BZOJ 2946 [Poi2000]公共串 后缀自动机
- BZOJ 2946: [Poi2000]公共串 后缀数组
- 【bzoj 2946】公共串(后缀自动机)
- bzoj 2946: [Poi2000]公共串 (后缀自动机)
- 后缀自动机 模板 【Poi2000】 公共串 bzoj 2946
- 【BZOJ 2946】【POI 2000】公共串【后缀数组】【裸】
- 【BZOJ】【P2946】【Poi2000】【公共串】【题解】【hash】
- [后缀自动机 模板题] SPOJ 1812Longest Common Substring II & BZOJ 2946 [Poi2000]公共串
- bzoj 最长公共子序列
- BZOJ 2423: [HAOI2010]最长公共子序列
- 2946: [Poi2000]公共串 二分+hash
- 【翻译自mos文章】Oracle Audit Vault 和Database Firewall 12.x 的平台支持情况
- STM32中使用printf打印串口数据
- js中this的用法
- (4.2.24)一种快速毛玻璃虚化效果实现
- 创建线程的几种方式
- [BZOJ 2946]公共串
- SGI STL :: Source Code Analysis :: stl_config.h
- java设计模式——结构型之享元模式
- 第3周项目3:输出星号图(2)
- 001.Pecdraw在绘制文本图元时异常-2015年12月28日
- 第三周项目2:本月有几天?
- scala学习之路:8.staticObject与伴生类伴生对象
- 输入一行数字,如果我们把这行数字中的‘5’都看成空格,那么就得到一行用空格分割的若干非负整数(可能有些整数以‘0’开头,这些头部的‘0’应该被忽略掉,除非这个整数就是由若干个‘0’组成的,这时这个整数
- Android 之联系人提供程序