D. Match & Catch suffix array

Source: Internet
Author: User

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

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

Given strings s1 and s2 consist of lowercase Latin letters, find the smallest (by Le ngth) Common substring p of both s1 and s2, where p I s 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?≤?| s 1|,?| s 2|? ≤?5000). Both strings consist of lowercase Latin letters.

Output

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

Examplesinput
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 am the ith Letter of the string.

We'll call string a l a l ? +?1 a l ? +?2 ... A R (1?≤? L ? ≤? r ? ≤?| a |) The substring [ l , R ] of the string a .

The substring [l,? R] is the 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.

For a long time did not write after the problem of the array, read the afternoon of the textbook review a bit ....

This problem is the base of the suffix array, the feeling is a set of templates. Paste the suffix array template below.

const int Maxn=1e4+5;int t[maxn],t2[maxn];int Sa[maxn],c[maxn];char s[maxn];int n;void build (int m) {int *x=t,*y=t2;    for (int i=0;i<m;i++) c[i]=0;    for (int i=0;i<n;i++) c[x[i]=s[i]]++;    for (int i=1;i<m;i++) c[i]+=c[i-1];    for (int i=n-1;i>=0;i--) sa[--c[x[i]]]=i;        for (int k=1;k<=n;k<<=1) {int p=0;        for (int i=n-k;i<n;i++) y[p++]=i;        for (int i=0;i<n;i++) if (sa[i]>=k) y[p++]=sa[i]-k;        Cardinal sort first keyword for (int i=0;i<m;i++) c[i]=0;        for (int i=0;i<n;i++) c[x[y[i]]]++;        for (int i=1;i<m;i++) c[i]+=c[i-1];        for (int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];        Swap (x, y); P=1;        x[sa[0]]=0;        for (int i=1;i<n;i++) x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;        if (p>=n) break;    M=p;    }}int rank[maxn],height[maxn];void getheight () {int k=0;    for (int i=0;i<n;i++) rank[sa[i]]=i;        for (int i=0;i<n;i++) {if (k) k--;if (rank[i]==0) {height[rank[i]]=0;        Continue        } int j=sa[rank[i]-1];        while (S[i+k]==s[j+k]) k++;    Height[rank[i]]=k; }}

D. Match & Catch suffix array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.