Codeforces round 244(div.2) D match&catch【字符串hash】

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.

题意是说,给定两个字符串,找到最小的相同串,同时这个串只能在两个串中出现一次。开始想可能是后缀数组,但是不太会,参考了一下别人的想法,发现字符串hash也可以做。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef unsigned __int64 ULL;
const int Maxn=6000;
const ULL seed=13331,length=10007;
struct node
{
	ULL val;
	int next;
	node(){};
	node(ULL v,int n) : val(v), next(n){};
};
ULL seedpow[Maxn] = {1ULL}, hash1[Maxn], hash2[Maxn];
node edge[Maxn<<1];
int head[length], cnt1[Maxn<<1], cnt2[Maxn<<1], sz;
char s[Maxn], t[Maxn]; 


void insert(ULL w,bool flag)
{
	int p=w%length;
	for(int i=head[p];i>=0;i=edge[i].next)
	{
		if(w!=edge[i].val) continue;
		if(flag==true) cnt1[i]++;
		else cnt2[i]++;
		return ;
	}
	cnt1[sz]=0;
	cnt2[sz]=0;
	if(flag) cnt1[sz]++;
	else cnt2[sz]++;
	edge[sz]=node(w,head[p]);
	head[p]=sz++;
	return ;
}




int main()
{
	scanf("%s%s",&s,&t);
	int ls=strlen(s);
	int lt=strlen(t);
	for(int i=1;i<=max(ls,lt);i++)
	{
		seedpow[i]=seedpow[i-1]*seed;
	}
	for(int i=1;i<=ls;i++)
	{
		hash1[i]=hash1[i-1]*seed+s[i-1];
	}
	for(int i=1;i<=lt;i++)
	{
		hash2[i]=hash2[i-1]*seed+t[i-1];
	}
	for(int l=1;l<=min(ls,lt);l++)
	{
		for(int i=0;i<length;i++) head[i]=-1;
		sz=0;
		for(int ed=l;ed<=ls;ed++)
		{
			insert(hash1[ed]-hash1[ed-l]*seedpow[l],true);
		}
		for(int ed=l;ed<=lt;ed++)
		{
			insert(hash2[ed]-hash2[ed-l]*seedpow[l],false);
		}
		for(int i=0;i<sz;i++)
		{
			if(cnt1[i]==1&&cnt2[i]==1) 
			{
				printf("%d\n",l);
				return 0;
			}
		}
	}
	printf("-1\n");
	return 0;
} 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值