Codeforces 427 D. Match & Catch

警方总部在不同频率水平上监听信号,并获取了来自两个不同频率的可疑编码字符串 s1 和 s2。他们怀疑这两个字符串分别来自两名罪犯,并正计划进行邪恶任务。现在,他们试图找出这两个字符串间的最小长度的独特共用子串,该子串仅在第一个字符串中出现一次,且在第二个字符串中也仅出现一次。本文将详细介绍如何通过输入两个由小写拉丁字母组成的字符串 s1 和 s2,找到它们间的最小独特共用子串的长度。


后缀数组....

在两个串中唯一出现的最小公共子串

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, wherep 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.

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.




#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=10100,INF=0x3f3f3f3f;

int sa[maxn],rank[maxn],rank2[maxn],h[maxn],c[maxn],*x,*y,ans[maxn];
char str[maxn];

bool cmp(int*r,int a,int b,int l,int n)
{
    if(r[a]==r[b]&&a+l<n&&b+l<n&&r[a+l]==r[b+l]) return true;
    return false;
}

bool radix_sort(int n,int sz)
{
    for(int i=0;i<sz;i++) c[i]=0;
    for(int i=0;i<n;i++) c[x[y[i]]]++;
    for(int i=1;i<sz;i++) c[i]+=c[i-1];
    for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
}

void get_sa(char c[],int n,int sz=128)
{
    x=rank,y=rank2;
    for(int i=0;i<n;i++) x[i]=c[i],y[i]=i;
    radix_sort(n,sz);
    for(int len=1;len<n;len*=2)
    {
        int yid=0;
        for(int i=n-len;i<n;i++) y[yid++]=i;
        for(int i=0;i<n;i++) if(sa[i]>=len) y[yid++]=sa[i]-len;

        radix_sort(n,sz);

        swap(x,y);
        x[sa[0]]=yid=0;

        for(int i=1;i<n;i++)
        {
            x[sa[i]]=cmp(y,sa[i],sa[i-1],len,n)?yid:++yid;
        }

        sz=yid+1;

        if(sz>=n) break;
    }

    for(int i=0;i<n;i++) rank[i]=x[i];
}

void get_h(char str[],int n)
{
    int k=0; h[0]=0;
    for(int i=0;i<n;i++)
    {
        if(rank[i]==0) continue;
        k=max(k-1,0);
        int j=sa[rank[i]-1];
        while(i+k<n&&j+k<n&&str[i+k]==str[j+k]) k++;
        h[rank[i]]=k;
    }
}

int main()
{
    cin>>str;
    int sg=strlen(str);
    str[sg]=127;
    cin>>str+sg+1;
    int n=strlen(str);
    get_sa(str,n);
    get_h(str,n);

    int ans=INF;
    int s1=0,s2=0,last=-1;
    for(int i=1;i<n;i++)
    {
        if(sa[i-1]<sg&&sa[i]<sg) continue;
        if(sa[i-1]>sg&&sa[i]>sg) continue;

        int pre=h[i-1];
        int next=h[i+1];
        if(h[i]>max(pre,next))
        {
            ans=min(ans,max(pre,next)+1);
        }
    }
    if(ans==INF) ans=-1;
    printf("%d\n",ans);
    return 0;
}



转载于:https://www.cnblogs.com/mengfanrong/p/3763720.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值