Tranvick

D

Nov 9th, 2011
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <cstdio>
  6. #include <algorithm>
  7. #include <cmath>
  8. #include <vector>
  9. #include <set>
  10. #include <stack>
  11. #include <map>
  12. #include <queue>
  13. #include <string>
  14. #include <memory.h>
  15. #include <iterator>
  16. #define ll long long
  17. #define ull unsigned long long
  18. #define ul unsigned long
  19. #define ld long double
  20. #define mem(A,X) memset(A,X,sizeof(A))
  21. #define memo(A) memset(A,0,sizeof(A))
  22. #define forn(I,B) for (I=1;I<=(B);I++)
  23. #define repp(I,B) for (int I=0;I<(B);I++)
  24. #define fornn(I,B) for (int I=1;I<=(B);I++)
  25. #define rep(I,B) for (I=0;I<(B);I++)
  26. #define sz(X) ((int)(X.size()))
  27. #define lg(X) ((int)(X.length()))
  28. #define cl(X) (X).clear()
  29. #define labs(X) (((X)>0)?(X):(-(X)))
  30. #define INF 1111111111
  31. #define ropen(X) freopen(X,"r",stdin)
  32. #define wopen(X) freopen(X,"w",stdout)
  33. #define bg(X) (X.begin())
  34. #define en(X) (X.end())
  35. #define vi vector<int>
  36. #define pii pair<int,int>
  37. #define pull pair<ull,ull>
  38. #define pb push_back
  39. #define mp make_pair
  40. #define all(X) (X).begin(),(X).end()
  41. #define N 1111111
  42. #define P1 31
  43. #define P2 29
  44.  
  45.                          
  46. using namespace std;      
  47.  
  48. string s;
  49. int n,p[N];
  50. pull h[N];
  51. ull p1[N],p2[N];
  52.  
  53. //    http://codeforces.ru/contest/127/problem/D
  54.  
  55. void init(){
  56.     cin>>s;
  57.     n=s.length();
  58.     p1[0]=p2[0]=1;
  59.     fornn(i,n-1){
  60.         p1[i]=p1[i-1]*P1;
  61.         p2[i]=p2[i-1]*P2;
  62.     }
  63. }
  64.  
  65. void kmp(){
  66.     p[0]=0;                        
  67.     for (int i=1;i<n;i++){
  68.         int j=p[i-1];
  69.         while (j>0 && s[i]!=s[j]) j=p[j-1];
  70.         if (s[i]==s[j]) ++j;
  71.         p[i]=j;
  72.     }
  73. }
  74.  
  75. void hash_calc(){
  76.         h[0].first=h[0].second=s[0]-'a'+1;
  77.     fornn(i,n-1){
  78.         h[i].first=h[i-1].first+(s[i]-'a'+1)*p1[i];
  79.         h[i].second=h[i-1].second+(s[i]-'a'+1)*p2[i];
  80.     }
  81. }
  82.  
  83. bool f(int m){
  84.     if (m==0) return 1;
  85.     ull h1=h[m-1].first,h2=h[m-1].second;
  86.     h1*=p1[n-m];
  87.     h2*=p2[n-m];
  88.     if ((h1!=h[n-1].first-h[n-m-1].first) || (h2!=h[n-1].second-h[n-m-1].second)) return 0;
  89.     for (int i=m-1;i<=n-m-1;i++) if (p[i]>=m) return 1;
  90.     return 0;
  91. }
  92.  
  93. void sol(){
  94.     int l=0,r=p[n-1];
  95.     int m=0;
  96.     while (l<r){
  97.         m=(l+r)/2;
  98.         if (f(m)) l=m+1;
  99.         else r=m;
  100.     }
  101.     if (f(p[n-1]))++r;
  102.     if (r<2){
  103.         cout<<"Just a legend"<<endl;
  104.         return;
  105.     }
  106.     repp(i,r-1) cout<<s[i];
  107.     cout<<endl;
  108. }
  109.  
  110. int main(){
  111.     ios_base::sync_with_stdio(0);
  112.     init();
  113.     kmp();
  114.     hash_calc();
  115.     sol();
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment