A9368 | Match & Catch
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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.
输入格式
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.
输出格式
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.
输入输出样例
输入 #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$ .
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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted