Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <string>
- #define ulli unsigned long long int
- #define optimizar_io ios_base::sync_with_stdio(0);cin.tie(0);
- using namespace std;
- int n, p;
- int P[1000002];
- ulli H[1000002], b[1000002];
- string A;
- ulli hashing( int i, int j ){
- if( i == 0 )
- return H[j];
- return H[j] - ( H[i - 1] * b[j - i + 1] );
- }
- bool possible( int l ){
- for( int i = 1; i + l < n - 1; i++ )
- if( H[l] == hashing( i, i + l ) )
- return true;
- return false;
- }
- int main(){
- optimizar_io
- cin >> A;
- n = A.size();
- b[0] = 1; b[1] = 29;
- for( int i = 2; i <= n; i++ )
- b[i] = b[i - 1] * b[1];
- H[0] = ( A[0] - 'a' );
- for( int i = 1; i < n; i++ )
- H[i] = H[i - 1] * b[1] + ( A[i] - 'a' );
- for( int i = 0; i < n; i++ )
- if( hashing( 0, i ) == hashing( n - i - 1, n - 1 ) )
- P[++p] = i;
- int b = 1, e = p, m;
- if( !possible( P[1] ) ){
- cout << "Just a legend\n";
- return 0;
- }
- while( b != e ){
- m = ( b + e ) / 2 + 1;
- if( possible( P[m] ) )
- b = m;
- else
- e = m - 1;
- }
- for( int i = 0; i <= P[b]; i++ )
- cout << A[i];
- cout << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement