Advertisement
VladSmirN

палиндром

Nov 22nd, 2020
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ld long double
  4. #define ll long long
  5. const int MAXN  = 239300;
  6. ll INF  = 9223372036854775000;
  7. ll MD  = 1000000009;
  8. ll L  = 53;
  9. ll MAXSIZE = 2000005;
  10. vector<ll>st(MAXSIZE,0);
  11. vector<ll> createHash(string s)
  12. {
  13.     vector<ll>h(s.size()+1,0);
  14.     for(int i=0; i<s.size(); ++i)
  15.     {
  16.         h[i+1] = (h[i]*L +s[i])%MD;
  17.     }
  18.     return h;
  19. }
  20. ll getHash(vector<ll>&h,ll l,ll r)
  21. {
  22.     return (h[r] - h[l-1]*st[r-l+1] +MD*MD)%MD;
  23. }
  24.  
  25. int main()
  26. {
  27.    ifstream file("data.txt");
  28.     ll N,M,S1,S2,LS;
  29.     file>>N;
  30.     string s1,s2;
  31.     file>>s2;
  32.     s1 = s2;
  33.     reverse(s2.begin(),s2.end());
  34.     st[0] = 1;
  35.     for(int i=1; i<MAXSIZE; ++i)
  36.     {
  37.         st[i] = (st[i-1]*L)%MD;
  38.     }
  39. //cout<<s1<<" "<<s2<<endl;
  40.     vector<ll>h1 = createHash(s1);
  41.     vector<ll>h2 = createHash(s2);
  42.     int c = 0;
  43.     int maxP = 1;
  44.     for(int i=1; i<=s1.size(); ++i)
  45.     {
  46.  
  47.  
  48.         int l,r,m;
  49.         l = i ;
  50.         r = s1.size()+1;
  51.         while(l+1<r){
  52.  
  53.            m = (l+r)/2;
  54.  cout<<i<<" "<<m<<" "<<s1.size() - m+1<<" "<< s1.size() - i+1<<" "<<(getHash(h1,i,m) == getHash(h2,s1.size() - m+1, s1.size() - i+1))<<endl;
  55.            if(getHash(h1,i,m) == getHash(h2,s1.size() - m+1, s1.size() - i+1)){
  56.                 l = m ;
  57.            }else{
  58.                 r = m;
  59.            }
  60.  
  61.         };
  62. cout<<-i+l+1<<endl;
  63.         maxP = max(maxP,-i+l+1);
  64.  
  65.  
  66.     }
  67.     cout<<maxP;
  68.  
  69.  
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement