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. Flask---ajax(jquery)交互

    目录结构如下: |--| |--run.py |--static |--test.txt |--templates |--index.html 前端代码如下: index.html <!DOCT ...

  2. HDU-2647 Reward ,逆拓排。

    Reward 发工资,以前看过这题,做没做忘了(应该是没做). 很明显的拓排.但数据范围这么大,吓得我当时就不敢动手.后来找题解发现还是相当于两层循环(are you kidding me?)当时卡在 ...

  3. ZOJ3860-Find the Spy

    Find the Spy Time Limit: 2 Seconds      Memory Limit: 65536 KB Whoooa! There is a spy in Marjar Univ ...

  4. [luoguP1169] [ZJOI2007]棋盘制作(单调栈)

    传送门 和玉蟾宫差不多 ——代码 #include <cstdio> #include <iostream> using namespace std; ; int n, m, ...

  5. 深入理解ajax系列第五篇

    前面的话 一般地,使用readystatechange事件探测HTTP请求的完成.XHR2规范草案定义了进度事件Progress Events规范,XMLHttpRequest对象在请求的不同阶段触发 ...

  6. ArrayList去除重复元素

    去除一个ArrayList的重复元素有两种方法:(ArrayList与Vector的存储结构是Object[],LinkedList是双向列表) 第一种是不需要借助临时list,用equals方法比较 ...

  7. x1 carbon 扩展屏 模糊

    x1 carbon 扩展屏 模糊,扩展屏是dell的屏,分辨率最大是1920*1080, x1最大是2560*1440. 不论是通过DP mini转VGA,还是HDMI,输出都是模糊,只有复制屏幕的时 ...

  8. Teamviewer ubuntu 提示 TeamViewer Daemon is not running

    http://blog.csdn.net/laohuang1122/article/details/12657343 Ubunut 12.04下面安装了Teamviewer,刚安装完启动是没有问题的, ...

  9. 【APUE】孤儿进程与僵死进程

    基本概念: 在unix/linux中,正常情况下,子进程是通过父进程创建的,子进程在创建新的进程.子进程的结束和父进程的运行是一个异步过程,即父进程永远无法预测子进程 到底什么时候结束. 当一个 进程 ...

  10. markdown 插入latex公式练习

    markdown 插入latex公式 $$公式$$表示行间公式,本来Tex中使用\(公式\)表示行内公式,但因为Markdown中\是转义字符,所以在Markdown中输入行内公式使用\\(公式\\) ...