Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <ctime>
- #include <cstdio>
- #include <algorithm>
- #include <cmath>
- #include <vector>
- #include <set>
- #include <stack>
- #include <map>
- #include <queue>
- #include <string>
- #include <memory.h>
- #include <iterator>
- #define ll long long
- #define ull unsigned long long
- #define ul unsigned long
- #define ld long double
- #define mem(A,X) memset(A,X,sizeof(A))
- #define memo(A) memset(A,0,sizeof(A))
- #define forn(I,B) for (I=1;I<=(B);I++)
- #define repp(I,B) for (int I=0;I<(B);I++)
- #define fornn(I,B) for (int I=1;I<=(B);I++)
- #define rep(I,B) for (I=0;I<(B);I++)
- #define sz(X) ((int)(X.size()))
- #define lg(X) ((int)(X.length()))
- #define cl(X) (X).clear()
- #define labs(X) (((X)>0)?(X):(-(X)))
- #define INF 1111111111
- #define ropen(X) freopen(X,"r",stdin)
- #define wopen(X) freopen(X,"w",stdout)
- #define bg(X) (X.begin())
- #define en(X) (X.end())
- #define vi vector<int>
- #define pii pair<int,int>
- #define pull pair<ull,ull>
- #define pb push_back
- #define mp make_pair
- #define all(X) (X).begin(),(X).end()
- #define N 1111111
- #define P1 31
- #define P2 29
- using namespace std;
- string s;
- int n,p[N];
- pull h[N];
- ull p1[N],p2[N];
- // http://codeforces.ru/contest/127/problem/D
- void init(){
- cin>>s;
- n=s.length();
- p1[0]=p2[0]=1;
- fornn(i,n-1){
- p1[i]=p1[i-1]*P1;
- p2[i]=p2[i-1]*P2;
- }
- }
- void kmp(){
- p[0]=0;
- for (int i=1;i<n;i++){
- int j=p[i-1];
- while (j>0 && s[i]!=s[j]) j=p[j-1];
- if (s[i]==s[j]) ++j;
- p[i]=j;
- }
- }
- void hash_calc(){
- h[0].first=h[0].second=s[0]-'a'+1;
- fornn(i,n-1){
- h[i].first=h[i-1].first+(s[i]-'a'+1)*p1[i];
- h[i].second=h[i-1].second+(s[i]-'a'+1)*p2[i];
- }
- }
- bool f(int m){
- if (m==0) return 1;
- ull h1=h[m-1].first,h2=h[m-1].second;
- h1*=p1[n-m];
- h2*=p2[n-m];
- if ((h1!=h[n-1].first-h[n-m-1].first) || (h2!=h[n-1].second-h[n-m-1].second)) return 0;
- for (int i=m-1;i<=n-m-1;i++) if (p[i]>=m) return 1;
- return 0;
- }
- void sol(){
- int l=0,r=p[n-1];
- int m=0;
- while (l<r){
- m=(l+r)/2;
- if (f(m)) l=m+1;
- else r=m;
- }
- if (f(p[n-1]))++r;
- if (r<2){
- cout<<"Just a legend"<<endl;
- return;
- }
- repp(i,r-1) cout<<s[i];
- cout<<endl;
- }
- int main(){
- ios_base::sync_with_stdio(0);
- init();
- kmp();
- hash_calc();
- sol();
- }
Advertisement
Add Comment
Please, Sign In to add comment