Codeforces Round #244 (Div. 2) D. Match & Catch

D. Match & Catch
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

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 pis 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.

Sample test(s)
input
apple
pepperoni
output
2
input
lover
driver
output
1
input
bidhan
roy
output
-1
input
testsetses
teeptes
output
3
Note

Imagine we have string a = a1a2a3...a|a|, where |a| is the length of string a, and ai is the ith letter of the string.

We will call string alal + 1al + 2...ar (1 ≤ l ≤ r ≤ |a|) the substring [l, r] of the string a.

The substring [l, r] is unique in a if and only if there is no pair l1, r1 such that l1 ≠ l and the substring [l1, r1] is equal to the substring[l, r] in a.

/*
枚举长度+后缀数组,我们先把两个字符串连接起来,然后跑
在判断 x 长度的字串是否满足时,我们去扫一次h数组
如果h 数组 >= x 且它是单峰的,还有属于两个不同字串,那么就是符合要求
*/
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std ;

#define maxn 100010
int s[maxn] , t[maxn] , t2[maxn] ;
int  c[maxn] , sa[maxn] ;
void build_sa( int n ,int m )
{
	int i , *x = t , *y = t2 , p ,j ;
	for( i = 0 ; i < m ;i++ )c[i] = 0 ;
	for( i = 0 ; i < n ;i++ )c[x[i]=s[i]]++ ;
	for( i = 1 ; i < m ;i++)c[i] += c[i-1] ;
	for( i = n-1 ; i >= 0 ;i--)
		sa[--c[x[i]]] = i ;
	for( j = 1 ; j < n ; j *= 2 )
	{
		p = 0 ;
		for( i = n-j ; i < n;i++)y[p++] = i ;
		for( i = 0 ; i < n ;i++)if( sa[i] >= j )
			y[p++] = sa[i]-j;
		for( i = 0 ; i < m;i++)c[i] = 0 ;
		for( i = 0 ; i < n ;i++)c[x[y[i]]]++ ;
		for( i = 1 ; i < m ;i++)c[i] += c[i-1] ;
		for( i = n-1 ; i >= 0 ;i-- )
			sa[--c[x[y[i]]]] = y[i] ;
		swap(x,y) ;
		p = 1 ; x[sa[0]] = 0 ;
		for( i = 1 ; i < n;i++)
			x[sa[i]] = ((y[sa[i-1]] == y[sa[i]])&&(y[sa[i-1]+j]==y[sa[i]+j])) ? p-1:p++ ;
		m = p ;
	}

}

int rank1[maxn] , h[maxn] ,n ;
void getH( int n )
{
	int i , j , k = 0 ;
	for( i = 1 ; i <= n ;i++)rank1[sa[i]] = i ;
 // h从1开始
	for( i = 0 ; i < n;i++)
	{
		if(k)k-- ;
		else k = 0;
		 j = sa[rank1[i]-1] ;
		while( s[i+k] == s[j+k])k++ ;
		h[rank1[i]] = k ;
	}
}
bool find( int x ,int nn)
{
    int i ,j, k ;
    for( i = 1 ; i <= nn;i++ )
    {
        if(h[i] >= x )
        {
            if((sa[i] < n && sa[i-1] > n ) ||(sa[i] > n && sa[i-1] < n ) )
            {
                if(h[i+1] < x && h[i-1] < x ) return true ;
            }
        }
    }
    return false ;
}
char a[maxn],b[maxn] ;
int main()
{
    int m,i,k,j,L,R;
    int mid ;
    while( scanf("%s%s",a,b) != EOF )
    {
        n = strlen(a) ;
        m = strlen(b) ;
        k = 0 ;
        for( i =  0 ; i < n ;i++ )
            s[k++] = a[i]-'a'+1 ;
        s[k++] = 27 ;
        for( i = 0 ; i < m ;i++ )
            s[k++] = b[i]-'a'+1 ;
        s[k] = 0 ;
        build_sa(k+1,40) ;
        getH(k) ;
        h[0] = 0 ;
        for( L = 1 ; L <= n ;L++ )
            if(find(L,k)) break ;
        if(L==n+1)L=-1 ;
        cout << L << endl;
    }
    return 0 ;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值