cf126b(kmp好题)

 

http://codeforces.com/contest/126/problem/B

#include<bits/stdc++.h>
using namespace std;
const int M = 1e6 + 10 ;
char s[M] ;
int lens ;
bool vis[M] ;
int NEXT[M] ;

void get_fail () {
	NEXT[0] = -1 ;
	for (int i = 1 , at = -1  ; i < lens ; i ++) {
		while (at != -1 && s[at+1] != s[i]) at = NEXT[at] ;
		NEXT[i] = s[at+1] == s[i] ? ++at : at ;
	}
}

void solve () {
	get_fail () ;
	int at = NEXT[lens-1] ; 
	while (at != -1) {
		vis[at] = 1 ;
		at = NEXT[at] ;
	}
	int maxn = -1 ;
	for (int i = lens-2 ; i >= 0 ; i --) {
		if (vis[NEXT[i]]) {
			if (NEXT[i] > maxn) maxn = NEXT[i] ;
		}
	}
	if (maxn == -1) puts ("Just a legend") ;
	for (int i = 0 ; i <= maxn ; i ++) printf ("%c" , s[i]) ; puts ("") ;
}

int main () {
	scanf ("%s" , s) ;
	lens = strlen (s) ;
	solve () ;
	return 0 ;
} 
posted @ 2015-11-22 18:39  92度的苍蓝  阅读(252)  评论(0)    收藏  举报
http://images.cnblogs.com/cnblogs_com/Running-Time/724426/o_b74124f376fc157f352acc88.jpg