Codeforces 427D Match & Catch (后缀数组)

70 篇文章 0 订阅
53 篇文章 0 订阅

D. Match & Catch

time limit per test: 1 second

memory limit per test: 512 megabytes

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.

Examples

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.

题目链接:http://codeforces.com/contest/427/problem/D

题目大意:给两个字符串s1,s2,求一个最短的公共子串,要求这个子串在s1,s2中均只出现过一次

题目分析:将两个字符串拼起来(用一个输入范围外的字符隔开,这么做是为了最终求解时能简单的区分出两个串),对拼接后的字符串求其height,求解时需要满足三个条件:

1. 子串分别在s1和s2:对枚举的i,必须满足sa[i]和sa[i-1]分别在len1的两侧

2. 子串在s1和s2中均只出现一次:必须满足height[i] > height[i - 1],height[i] > height[i + 1],即suffix[sa[i]]和suffix[sa[i-1]]的lcp长度必须比相邻名次的对应值大,这样才能保证只出现一次

3. 要求的是最短的,最短的必然是max(height[i - 1], height[i + 1]) + 1,这里不能直接取height[i],因为height[i]不一定是最短的

比如假设

suffix[sa[1]] = "ababc",

suffix[sa[2]] = "abacd",

suffix[sa[3]] = "abacdbc"

suffix[sa[4]] = "abc"

则height[2] = 3, height[3] = 5, height[4] = 2,这时候suffix[sa[2]]和suffix[sa[3]]有lcp "abacd",长度为5,但其实"abac"已经是唯一的了,所以这里应该求出的是max(height[2], height[4])+1 = 4

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 1e4 + 5;
char s[MAX], s1[MAX >> 1], s2[MAX >> 1];
int n, m, sa[MAX], height[MAX], len1, len2;
int rk[MAX], tp[MAX], tax[MAX];

bool cmp(int* r, int a, int b, int k) {
    return r[a] == r[b] && r[a + k] == r[b + k];
}

void radix_sort() {
    for (int i = 0; i <= m; i++) {
        tax[i] = 0;
    }
    for (int i = 1; i <= n; i++) {
        tax[rk[tp[i]]]++;
    }
    for (int i = 1; i <= m; i++) {
        tax[i] += tax[i - 1];
    }
    for (int i = n; i >= 1; i--) {
        sa[tax[rk[tp[i]]]--] = tp[i];
    }
}

void get_sa() {
    for (int i = 1; i <= n; i++) {
        rk[i] = s[i];
        tp[i] = i;
        m = max(m, (int)s[i]);
    }
    radix_sort();
    for (int j = 1, p = 0; p < n; j <<= 1, m = p) {
        p = 0;
        for (int i = n - j + 1; i <= n; i++) {
            tp[++p] = i;
        }
        for (int i = 1; i <= n; i++) {
            if (sa[i] > j) {
                tp[++p] = sa[i] - j;
            }
        }
        radix_sort();
        swap(rk, tp);
        rk[sa[1]] = p = 1;
        for (int i = 2; i <= n; i++) {
            rk[sa[i]] = cmp(tp, sa[i], sa[i - 1], j) ? p : ++p;
        }
    }
}

void get_height() {
    for (int i = 1, j = 0; i <= n; i++) {
        if (j) {
            j--;
        }
        int prevPos = sa[rk[i] - 1];
        while (i + j <= n && prevPos + j <= n && s[i + j] == s[prevPos + j]) {
            j++;
        }
        height[rk[i]] = j;
    }
}

int main() {
    scanf("%s%s", s1 + 1, s2 + 1);
    len1 = strlen(s1 + 1);
    len2 = strlen(s2 + 1);
    for (int i = 1; i <= len1; i++) {
        s[i] = s1[i];
    }
    s[len1 + 1] = ' ';
    for (int i = 1; i <= len2; i++) {
        s[len1 + 1 + i] = s2[i];
    }
    s[len1 + len2 + 2] = '\0';
    n = strlen(s + 1);
    get_sa();
    get_height();
    int ans = n;
    for (int i = 1; i <= n; i++) {
        int miPos = min(sa[i], sa[i - 1]);
        int maPos = max(sa[i], sa[i - 1]);
        if (miPos <= len1 && maPos > len1) {
            if (height[i] > height[i - 1] && height[i] > height[i + 1])
            ans = min(ans, max(height[i - 1], height[i + 1]) + 1);
        }
    }
    printf("%d\n", ans == n ? -1 : ans);
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值