Passward
9.11
思路:
kmp跑一边,从最后向前跳next,标记一下中间的串就行啦。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
const int N = 100010;
char s[N];
int lens, nxt[N];
bool flag[N];
void calcnext() {
int i = 0, j = -1;
nxt[0] = -1;//
while(i < lens) {
if(j == -1 || s[i] == s[j]) {
i++, j++;
nxt[i] = j;
}
else
j = nxt[j];
}
}
int main(){
freopen ("passward.in", "r", stdin);
freopen ("passward.out", "w", stdout);
scanf("%s", s);
lens = strlen(s);
calcnext();
for(register int i=1; i<lens; i++) flag[nxt[i]] = true;//只有前缀和后缀的子串不会被标记
register int i;
for( i=nxt[lens]; i; i=nxt[i])//lens
if( flag[i] ) break;//越早break,串越长
if( !i ) printf("Just a legend");
else for(register int j=0; j<i; j++) putchar( s[j] );
return 0;
}