CF427D.Match & Catch

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Police headquarter is monitoring signal on different frequency levels. They have got two suspiciously encoded strings _s_1 and _s_2 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 _s_1 and _s_2 consist of lowercase Latin letters, find the smallest (by length) common substring p of both _s_1 and _s_2, where p is a unique substring in _s_1 and also in _s_2. See notes for formal definition of substring and uniqueness.

警方总部正在监控不同频率级别的信号。他们从两个不同频率上获取了两段可疑编码的字符串 s1s_1 和 s2s_2 作为信号。他们怀疑这两段字符串分别来自两名不同的罪犯,且他们正密谋实施某种邪恶行动。

现在,警方试图在两段字符串中找出一个最短的公共子串。该子串必须在第一段字符串中仅出现一次,同时也必须在第二段字符串中仅出现一次。

给定两个仅由小写拉丁字母组成的字符串 s1s_1 和 s2s_2,请找出它们的一个长度最小的公共子串 pp,使得 pp 在 s1s_1 中是唯一出现的子串,且在 s2s_2 中也是唯一出现的子串。子串及“唯一性”的形式化定义详见注释部分。

输入格式

The first line of input contains _s_1 and the second line contains _s_2 (1 ≤ |_s_1|, |_s_2| ≤ 5000). Both strings consist of lowercase Latin letters.

输入的第一行包含字符串 s1s_1,第二行包含字符串 s2s_2(1 ≤ ∣s1∣, ∣s2∣ ≤ 50001 \le |s_1|, |s_2| \le 5000)。两个字符串均由小写拉丁字母组成。

输出格式

Print the length of the smallest common unique substring of _s_1 and _s_2. If there are no common unique substrings of _s_1 and _s_2 print -1.

输出字符串 s1s_1 和 s2s_2 的最短公共唯一子串的长度。如果 s1s_1 和 s2s_2 不存在公共唯一子串,则输出 -1。

输入输出样例

  • 输入#1

    apple
    pepperoni

    输出#1

    2
  • 输入#2

    lover
    driver

    输出#2

    1
  • 输入#3

    bidhan
    roy

    输出#3

    -1
  • 输入#4

    testsetses
    teeptes

    输出#4

    3

说明/提示

Imagine we have string a = _a_1_a_2_a_3...a|a|, where |a| is the length of string a, and a__i is the i__th letter of the string.

We will 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 unique in a if and only if there is no pair _l_1, _r_1 such that _l_1 ≠ l and the substring [_l_1, _r_1] is equal to the substring [l, r] in a.

假设我们有一个字符串 a=a1a2a3…a∣a∣a = a_1a_2a_3\ldots a_{|a|},其中 ∣a∣|a| 表示字符串 aa 的长度,aia_i 表示该字符串的第 ii 个字符。

我们将字符串 alal+1al+2…ara_l a_{l+1} a_{l+2} \ldots a_r(其中 1≤l≤r≤∣a∣1 \le l \le r \le |a|)称为字符串 aa 的子串 [l, r][l,\,r]。

当且仅当不存在另一对下标 l1, r1l_1,\,r_1 满足 l1≠ll_1 \ne l,且子串 [l1, r1][l_1,\,r_1] 在 aa 中与子串 [l, r][l,\,r] 完全相等时,子串 [l, r][l,\,r] 在 aa 中被称为唯一的。

输入解题思路,AI测评打分。不知道怎么写?

首页