题目地址:
https://www.luogu.com.cn/problem/CF126B
题面翻译:
Asterix,Obelix和他们的临时伙伴Suffix、Prefix已经最终找到了和谐寺。然而和谐寺大门紧闭,就连Obelix的运气也没好到能打开它。不久他们发现了一个字符串
S
S
S(
∣
S
∣
<
=
1000000
|S|<=1000000
∣S∣<=1000000),刻在和谐寺大门下面的岩石上。Asterix猜想那一定是打开寺庙大门的密码,于是就大声将字符串朗读了出来,然而并没有什么事发生。于是Asterix又猜想密码一定是字符串
S
S
S的子串
T
T
T。Prefix认为
T
T
T是
S
S
S的前缀,Suffix认为
T
T
T是
S
S
S的后缀,Obelix却认为
T
T
T应该是
S
S
S中的某一部分,也就是说,
T
T
T既不是
S
S
S的前缀,也不是
S
S
S的后缀。Asterix选择子串
T
T
T来满足所有伙伴们的想法。同时,在所有可以被接受的子串变形中,Asterix选择了最长的一个(因为Asterix喜欢长的字符串)当Asterix大声读出子串T时,寺庙的大门开了。(也就是说,你需要找到既是
S
S
S的前缀又是
S
S
S的后缀同时又在
S
S
S中间出现过的最长子串)。现在给你字符串
S
S
S,你需要找到满足上述要求的子串
T
T
T。
输入格式:
一个长度在
[
1
,
1000000
]
[1,1000000]
[1,1000000]间的只包含小写字母的字符串S。
输出格式:
输出子串
T
T
T,如果
T
T
T不存在,输出 “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 s s s , 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 t t t of the string s s s .
Prefix supposed that the substring t t t is the beginning of the string s s s ; Suffix supposed that the substring t t t should be the end of the string s s s ; and Obelix supposed that t t t should be located somewhere inside the string s s s , that is, t t t is neither its beginning, nor its end.
Asterix chose the substring t t t 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 t t t aloud, the temple doors opened.
You know the string s s s . Find the substring t t t 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
s
s
s whose length can vary from
1
1
1 to
1
0
6
10^{6}
106 (inclusive), consisting of small Latin letters.
输出格式:
Print the string
t
t
t . If a suitable
t
t
t string does not exist, then print “Just a legend” without the quotes.
其实就是要找 s s s的一个子串 t t t,满足 t t t是 s s s的前缀,也是后缀,并且在中间也出现过。
考虑对 s s s求出KMP算法的 n e ne ne数组,先证明,既是 s s s的真前缀也是 s s s的真后缀的子串长度 l l l,一定满足 ∃ k , l = n e k [ n ] \exists k, l = ne^k[n] ∃k,l=nek[n]。证明依赖于KMP算法的正确性,因为KMP算法的正确性,主串的当前字符一定会逐次匹配 n e k [ n ] + 1 , k = 1 , 2 , . . . ne^k[n]+1,k=1,2,... nek[n]+1,k=1,2,...,那么既然 l l l是 s s s的某个相等的真前后缀的长度,一定会失配若干次就会让 l + 1 l+1 l+1去匹配,从而 ∃ k , l = n e k [ n ] \exists k, l = ne^k[n] ∃k,l=nek[n]。
那么算法就是这样的,首先求 n e ne ne数组,然后看一下 n e [ n ] ne[n] ne[n],如果 n e [ n ] = 0 ne[n]=0 ne[n]=0,说明不存在相等的真前后缀,输出"Just a legend"。否则的话,看一下 l = max i = 2 , 3 , . . . , n − 1 n e [ i ] l=\max_{i=2,3,...,n-1} ne[i] l=maxi=2,3,...,n−1ne[i],那么易知长 ≤ l \le l ≤l的前缀在中间一定存在(并且 > l >l >l的前缀在中间一定不存在),所以只需要求 max k n e k [ n ] ≤ l \max_k ne^k[n]\le l maxknek[n]≤l即可,即对 n n n取若干次 n e ne ne使得结果第一次小于等于 l l l。当然如果最后结果为 0 0 0,也说明无解。代码如下:
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1000010;
int n, ne[N], maxl;
char s[N];
// 求ne数组
void build_ne() {
for (int i = 2, j = 0; i <= n; i++) {
while (j && s[i] != s[j + 1]) j = ne[j];
if (s[i] == s[j + 1]) j++;
ne[i] = j;
if (i < n) maxl = max(maxl, ne[i]);
}
}
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
build_ne();
// 求最长相等的真前后缀长度
int len = ne[n];
if (!len) puts("Just a legend");
else {
// 找到最大的小于等于maxl的相等真前后缀长度
while (len > maxl) len = ne[len];
if (!len) puts("Just a legend");
else for (int i = 1; i <= len; i++) putchar(s[i]);
}
}
时空复杂度 O ( ∣ S ∣ ) O(|S|) O(∣S∣)。