首页 技术 正文
技术 2022年11月18日
0 收藏 625 点赞 2,551 浏览 1679 个字

Description

        给出几个由小写字母构成的单词,求它们最长的公共子串的长度。任务:l        读入单词l        计算最长公共子串的长度l        输出结果 

Input

 文件的第一行是整数 n,1<=n<=5,表示单词的数量。接下来n行每行一个单词,只由小写字母组成,单词的长度至少为1,最大为2000。 

Output

仅一行,一个整数,最长公共子串的长度。 

Sample Input

3
abcb
bca
acbc

Sample Output

HINT

 

Source

其实这题我没A因为权限号密码忘了 但是在COGS过了那就发一下吧。。  已AC

感觉很zz啊,和单个串有什么区别。

那就直接把除了第一个串之外的SAM建出来然后枚举第一个串暴力匹配求最小就好了

1Ahhh

update:

??为什么网上的题解都和我不一样??

这题好像只建一个SAM就行了??!!感觉自己好菜啊。。。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = * , INF = 1e9 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
struct SuffixAut {
int fa[MAXN], len[MAXN], ch[MAXN][], tot, last, root, ans, cur;
SuffixAut() { cur = tot = last = root = ; ans = ;}
void insert(int x) {
int now = ++tot, pre = last; last = now;
len[now] = len[pre] + ;
for(; pre && !ch[pre][x]; pre = fa[pre]) ch[pre][x] = now;
if(!pre) fa[now] = root;
else {
int q = ch[pre][x];
if(len[q] == len[pre] + ) fa[now] = q;
else {
int nows = ++tot;
memcpy(ch[nows], ch[q], sizeof(ch[q]));
fa[nows] = fa[q]; fa[q] = fa[now] = nows; len[nows] = len[pre] + ;
for(; pre && ch[pre][x] == q; pre = fa[pre]) ch[pre][x] = nows;
}
}
}
int work(int x) {
if(ch[cur][x]) {cur = ch[cur][x]; ans++; return ans;}
for(; cur && !ch[cur][x]; cur = fa[cur]);
if(!cur) cur = root, ans = ;
else ans = len[cur] + , cur = ch[cur][x];
return ans;
}
}Suf[];
char s[][MAXN];
int N[];
int main() {
#ifdef WIN32
freopen("a.in", "r", stdin);
#else
freopen("pow.in", "r", stdin);
freopen("pow.out", "w", stdout);
#endif
int num = read();
for(int i = ; i <= num; i++) scanf("%s", s[i] + ), N[i] = strlen(s[i] + );
for(int i = ; i <= num; i++)
for(int j = ; j <= N[i]; j++)
Suf[i].insert(s[i][j] - 'a');
int out = ;
for(int i = ; i <= N[]; i++) {
int ans = INF;
for(int j = ; j <= num; j++)
ans = min(ans, Suf[j].work(s[][i] - 'a'));
out = max(out, ans);
}
printf("%d", out);
return ;
}
上一篇: 淘宝cnpm
下一篇: chosen.jquery.js
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898