CF126B
Password
题面翻译
Asterix,Obelix 和他们的临时伙伴 Suffix、Prefix 已经最终找到了和谐寺。然而和谐寺大门紧闭,就连 Obelix 的运气也没好到能打开它。
不久他们发现了一个字符串
,刻在和谐寺大门下面的岩石上。Asterix 猜想那一定是打开寺庙大门的密码,于是就大声将字符串朗读了出来,然而并没有什么事发生。于是 Asterix 又猜想密码一定是字符串 的子串 。 Prefix 认为
是 的前缀,Suffix 认为 是 的后缀,Obelix 却认为 应该是 中的某一部分,也就是说, 既不是 的前缀,也不是 的后缀。 Asterix 选择子串
来满足所有伙伴们的想法。同时,在所有可以被接受的子串变形中,Asterix 选择了最长的一个。当 Asterix 大声读出子串 时,寺庙的大门开了。(也就是说,你需要找到既是 的前缀又是 的后缀同时又在 中间出现过的最长子串) 现在给你字符串
,你需要找到满足上述要求的子串 。 输入格式
一行一个只包含小写字母的字符串
。 输出格式
输出子串
,如果 不存在,输出 Just a legend
。题目描述
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them.
A little later they found a string
, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring of the string . Prefix supposed that the substring
is the beginning of the string ; Suffix supposed that the substring should be the end of the string ; and Obelix supposed that should be located somewhere inside the string , that is, is neither its beginning, nor its end. Asterix chose the substring
so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring aloud, the temple doors opened. You know the string
. Find the substring or determine that such substring does not exist and all that's been written above is just a nice legend. 输入格式
You are given the string
whose length can vary from to (inclusive), consisting of small Latin letters. 输出格式
Print the string
. If a suitable string does not exist, then print "Just a legend" without the quotes. 样例 #1
样例输入 #1
fixprefixsuffix
样例输出 #1
fix
样例 #2
样例输入 #2
abcdabc
样例输出 #2
Just a legend
看到这题的第一眼,想到的肯定是KMP(因为需要求最长公共前后缀嘛,将其记作这样就一目了然了吧,
那么我们只要
但是,实际上可能是找不到的(看题意就知道了),如果
可能有点混乱,将上述的步骤总结一下: 1、先预处理出
我们可以在第二步开始前加上特判
AC代码
|