Police headquarter is monitoring signal on different frequency levels. They have got two suspiciously encoded strings s1 and s2 from two different frequencies as signals. They are suspecting that these two strings are from two different criminals and they are planning to do some evil task.

Now they are trying to find a common substring of minimum length between these two strings. The substring must occur only once in the first string, and also it must occur only once in the second string.

Given two strings s1 and s2 consist of lowercase Latin letters, find the smallest (by length) common substring p of both s1 and s2, where p is a unique substring in s1 and also in s2. See notes for formal definition of substring and uniqueness.

Input

The first line of input contains s1 and the second line contains s2 (1 ≤ |s1|, |s2| ≤ 5000). Both strings consist of lowercase Latin letters.

Output

Print the length of the smallest common unique substring of s1 and s2. If there are no common unique substrings of s1 and s2 print -1.

Example

Input
apple
pepperoni
Output
2
Input
lover
driver
Output
1
Input
bidhan
roy
Output
-1
Input
testsetses
teeptes
Output
3

题意:给定字符串S,T。求最短的公共字串s的长度|s|,s满足在各自的母串里只出现了一次。

思路:建立S的后缀自动机,拓扑得到每个字串出现的次数num1。 然后T在后缀自动机上面走,也得到字串T的次数num2。 然后每个集合如果在各自串都只出现一次,就可以用当前集合的最小长度比较答案了。

经验:对建立后缀自动机的S串,在设置num1的时候,可以在np处设置num1=1(也就是所有Blue节点),或者建立完自动机后跑一遍,跑到的地方num1=1。

具体的可以参考代码。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int inf=;
const int maxn=;
int ans=inf,L; char c[maxn];
struct Sam
{
int fa[maxn],ch[maxn][],maxlen[maxn],Last,cnt;
int a[maxn],b[maxn],num1[maxn],num2[maxn],np,nq,p,q;
Sam(){
Last=cnt=;
}
void add(int x)
{
np=++cnt; p=Last; Last=np;
maxlen[np]=maxlen[p]+; num1[np]++;//方法1
while(p&&!ch[p][x]) ch[p][x]=np,p=fa[p];
if(!p) fa[np]=;
else {
q=ch[p][x];
if(maxlen[p]+==maxlen[q]) fa[np]=q;
else {
nq=++cnt; maxlen[nq]=maxlen[p]+;
fa[nq]=fa[q]; fa[q]=fa[np]=nq;
memcpy(ch[nq],ch[q],sizeof(ch[nq]));
while(p&&ch[p][x]==q) ch[p][x]=nq,p=fa[p];
}
}
}
void sort()
{
for(int i=;i<=cnt;i++) a[maxlen[i]]++;
for(int i=;i<=cnt;i++) a[i]+=a[i-];
for(int i=;i<=cnt;i++) b[a[maxlen[i]]--]=i;
//for(int i=1,tp=1;i<=cnt;i++){
// tp=ch[tp][c[i]-'a']; num1[tp]++;
//}
//方法2
for(int i=cnt;i>=;i--) num1[fa[b[i]]]+=num1[b[i]];
}
void solve()
{
scanf("%s",c+); L=strlen(c+); p=;
for(int i=;i<=L;i++){
int x=c[i]-'a';
if(ch[p][x]) p=ch[p][x];
else {
while(p&&!ch[p][x]) p=fa[p];
if(!p) p=;
else p=ch[p][x];
}
num2[p]++;
}
for(int i=cnt;i>=;i--) num2[fa[b[i]]]+=num2[b[i]];
for(int i=;i<=cnt;i++)
if(num1[i]==&&num2[i]==)
ans=min(ans,maxlen[fa[i]]+);
if(ans==inf) ans=-;
}
}sam;
int main()
{
scanf("%s",c+); L=strlen(c+);
for(int i=;i<=L;i++) sam.add(c[i]-'a');
sam.sort(); sam.solve();
printf("%d\n",ans);
return ;
}

CodeForces-427D:Match & Catch (后缀自动机)的更多相关文章

  1. D. Match & Catch 后缀自动机 || 广义后缀自动机

    http://codeforces.com/contest/427/problem/D 题目是找出两个串的最短公共子串,并且在两个串中出现的次数只能是1次. 正解好像是dp啥的,但是用sam可以方便很 ...

  2. codeforces 427D Match & Catch(后缀数组,字符串)

    题目 参考:http://blog.csdn.net/xiefubao/article/details/24934617 题意:给两个字符串,求一个最短的子串.使得这个子串在两个字符串中出现的次数都等 ...

  3. CodeForces 427D Match & Catch

    洛谷题目页面传送门 & CodeForces题目页面传送门 给定\(2\)个字符串\(a,b,|a|=n,|b|=m\),求最长的既在\(a\)中出现恰好\(1\)次又在\(b\)中出现恰好\ ...

  4. Codeforces 235C Cyclical Quest - 后缀自动机

    Some days ago, WJMZBMR learned how to answer the query "how many times does a string x occur in ...

  5. Codeforces 452E Three Strings(后缀自动机)

    上学期很认真地学了一些字符串的常用工具,各种 suffix structre,但是其实对后缀自动机这个部分是理解地不太透彻的,以致于看了师兄A这题的代码后,我完全看不懂,于是乎重新看回一些学习后缀自动 ...

  6. Codeforces 427D Match &amp; Catch(后缀自动机)

    [题目链接] http://codeforces.com/problemset/problem/427/D [题目大意] 给出一个两个字符串,求出最短且在两个字符串中唯一的公共子串. [题解] 以原字 ...

  7. CF #244 D. Match & Catch 后缀数组

    题目链接:http://codeforces.com/problemset/problem/427/D 大意是寻找两个字符串中最短的公共子串,要求子串在两个串中都是唯一的. 造一个S#T的串,做后缀数 ...

  8. Codeforces.700E.Cool Slogans(后缀自动机 线段树合并 DP)

    题目链接 \(Description\) 给定一个字符串\(s[1]\).一个字符串序列\(s[\ ]\)满足\(s[i]\)至少在\(s[i-1]\)中出现过两次(\(i\geq 2\)).求最大的 ...

  9. 后缀自动机(SAM)

    *在学习后缀自动机之前需要熟练掌握WA自动机.RE自动机与TLE自动机* 什么是后缀自动机 后缀自动机 Suffix Automaton (SAM) 是一个用 O(n) 的复杂度构造,能够接受一个字符 ...

  10. CF 427D Match &amp; Catch 求最短唯一连续LCS

    题目来源:CF 427D Match & Catch 题意:给出2个字符串 求最短的连续的公共字符串 而且该字符串在原串中仅仅出现一次 思路:把2个字符串合并起来求height 后缀数组hei ...

随机推荐

  1. ms sqlserver数据库建索引

    索引分类:从物理结构上可分为两种:聚集索引和非聚集索引 (此外还有空间索引.筛选索引.XML索引) 因为聚集索引是索引顺序与物理存储顺序一致,所以只能建一个. 聚集索引就是把数据按主键顺序存储: 因为 ...

  2. ZOJ - 3781 Paint the Grid Reloaded 题解

    题目大意: 给一个n*m的X O构成的格子,对一个点操作可以使与它相连通的所有一样颜色的格子翻转颜色(X—>O或O—>X),问给定的矩阵最少操作多少次可以全部变成一样的颜色. 思路: 1. ...

  3. HDU1213最简单的并查集问题

    题目地址 http://acm.hdu.edu.cn/showproblem.php?pid=1213 #include<iostream> using namespace std; #d ...

  4. Washing Clothes(poj 3211)

    大体题意:有n件衣服,m种颜色,某人和他的女炮一起洗衣服,必须一种颜色洗完,才能洗另一种颜色,每件衣服都有时间,那个人洗都一样,问最少用时. poj万恶的C++和G++,害得我CE了三次 /* 背包啊 ...

  5. Pollard rho模板

    #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #in ...

  6. Codeforces Round #298 (Div. 2) D. Handshakes [贪心]

    传送门 D. Handshakes time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  7. Spring Boot使用Feign客户端调用远程服务时出现:timed-out and no fallback available,failed and no fallback available的问题解决

    timed-out and no fallback available: 这个错误基本是出现在Hystrix熔断器,熔断器的作用是判断该服务能不能通,如果通了就不管了,调用在指定时间内超时时,就会通过 ...

  8. Java描述符(修饰符)的类型

    以下内容引用自http://wiki.jikexueyuan.com/project/java/modifier-types.html: 描述符(修饰符)是添加到那些定义中来改变他们的意思的关键词.J ...

  9. 前端开发数据mock神器 -- xl_mock

    1.为什么要实现数据 mock 要理解为什么要实现数据 mock,我们可以提供几个场景来解释, 1.现在的开发很多都是前后端分离的模式,前后端的工作是不同的,当我们前端界面已经完成,但是后端的接口迟迟 ...

  10. Scala入门到精通——第二十四节 高级类型 (三)

    作者:摆摆少年梦 视频地址:http://blog.csdn.net/wsscy2004/article/details/38440247 本节主要内容 Type Specialization Man ...