Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4. #define ulli unsigned long long int
  5. #define optimizar_io ios_base::sync_with_stdio(0);cin.tie(0);
  6. using namespace std;
  7.  
  8. int n, p;
  9. int P[1000002];
  10. ulli H[1000002], b[1000002];
  11. string A;
  12.  
  13. ulli hashing( int i, int j ){
  14. if( i == 0 )
  15. return H[j];
  16. return H[j] - ( H[i - 1] * b[j - i + 1] );
  17. }
  18.  
  19. bool possible( int l ){
  20. for( int i = 1; i + l < n - 1; i++ )
  21. if( H[l] == hashing( i, i + l ) )
  22. return true;
  23. return false;
  24. }
  25.  
  26. int main(){
  27.  
  28. optimizar_io
  29. cin >> A;
  30. n = A.size();
  31.  
  32. b[0] = 1; b[1] = 29;
  33. for( int i = 2; i <= n; i++ )
  34. b[i] = b[i - 1] * b[1];
  35.  
  36. H[0] = ( A[0] - 'a' );
  37. for( int i = 1; i < n; i++ )
  38. H[i] = H[i - 1] * b[1] + ( A[i] - 'a' );
  39.  
  40. for( int i = 0; i < n; i++ )
  41. if( hashing( 0, i ) == hashing( n - i - 1, n - 1 ) )
  42. P[++p] = i;
  43.  
  44. int b = 1, e = p, m;
  45.  
  46. if( !possible( P[1] ) ){
  47. cout << "Just a legend\n";
  48. return 0;
  49. }
  50.  
  51. while( b != e ){
  52. m = ( b + e ) / 2 + 1;
  53. if( possible( P[m] ) )
  54. b = m;
  55. else
  56. e = m - 1;
  57. }
  58.  
  59. for( int i = 0; i <= P[b]; i++ )
  60. cout << A[i];
  61. cout << "\n";
  62.  
  63.  
  64. return 0;
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement