[KMP][Hash] #93 div1 cf 126B Password
@(ACM题目)[KMP,字符串Hash]
Description
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, 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 of the string s.
Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end.
Asterix chose the substring 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 aloud, the temple doors opened.
You know the string s. Find the substring t or determine that such substring does not exist and all that’s been written above is just a nice legend.
Input
You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters.
Output
Print the string t. If a suitable t string does not exist, then print “Just a legend” without the quotes.
Sample
Input
fixprefixsuffix
Output
fix
Input
abcdabc
Output
Just a legend
题目分析
本题给定一个字符串 s ,求最长的子串,使得其同时为原字符串的前缀、后缀,并且去原字符串掉第一个和最后一个字母后,该子串仍然存在。无解输出“Just a legend”。
此题利用KMP中的失配函数,通过可以得到以当前位置的“前缀与后缀相同”的最大长度,见下图:
字符串开始的下标定为1。以最后一个字母d为例,当d失配后,其前方的后缀“ab”是整个字符串的前缀,故失配函数 lost6 (即字母d失配后要匹配的位置)为3(即字母c)。故此时“前缀与后缀相同”的最大长度为“ab”的长度2。
遍历字符串的每个位置 i ,通过失配函数 losti 找到当前最长公共前、后缀 s[1,losti−1] ,在用字符串Hash来check一下该子串是否是原字符串整个串的后缀即可。
代码
#include<bits/stdc++.h>
typedef long long LL;
typedef unsigned long long ull;
using namespace std;
const int maxn = 1e6+7;
const ull base = 163;
char s[maxn];
ull hah[maxn];
ull pw[maxn];
int n, lost[maxn];
void getFail(char* P)
{
int m = strlen(P+1);
lost[1] = lost[2] = 1;
for(int i = 2; i <= m; i++)
{
int j = lost[i];
while(j > 1 && P[i] != P[j]) j = lost[j];
lost[i+1] = P[i] == P[j] ? j+1 : 1;
}
}
void calcHash(char* s)
{
pw[0] = 1;
hah[0] = 0;
for(int i = 1; i <= n; i++)
{
pw[i] = pw[i-1] * base;
hah[i] = hah[i-1] * base + s[i];
}
}
ull getHash(int l, int r)
{
return hah[r] - hah[l-1] * pw[r-l+1];
}
int main()
{
scanf("%s", s+1);
n = strlen(s+1);
calcHash(s);
getFail(s);
int p = 0;
for(int i = 1; i <= n; ++i)
{
int l = lost[i] - 1;
if(l > p && getHash(1, l) == getHash(n-l+1, n)) p = l;
}
if(p > 0)
{
s[p+1] = '\0';
puts(s+1);
}
else
puts("Just a legend");
return 0;
}