Advertisement
Korotkodul

ХЭШИ H

Sep 20th, 2022 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int,int>
  11. #define pb(x) push_back(x)
  12. using namespace std;
  13. using ll = long long;
  14. using ld = long double;
  15. using db = double;
  16. void cv(vector <int> &v){
  17.     for (auto x: v) cout<<x<<' ';
  18.     cout<<"\n";
  19. }
  20.  
  21. void cvl(vector <ll> &v){
  22.     for (auto x: v) cout<<x<<' ';
  23.     cout<<"\n";
  24. }
  25.  
  26.  
  27. void cvv(vector <vector <int> > &v){
  28.     for (auto x: v) cv(x);
  29.     cout<<"\n";
  30. }
  31.  
  32. void cvb(vector <bool> v){
  33.     for (bool x: v) cout<<x<<' ';
  34.     cout<<"\n";
  35. }
  36.  
  37. void cvs(vector <string>  v){
  38.     for (auto a: v){
  39.         cout<<a<<"\n";
  40.     }
  41. }
  42.  
  43. void cvp(vector <pii> a){
  44.     for (auto p: a){
  45.         cout<<p.first<<' '<<p.second<<"\n";
  46.     }
  47.     cout<<"\n";
  48. }
  49.  
  50. ll p =101,mod=1e9+7;
  51. bool sh=0;
  52. /*
  53. 1101
  54. 1011
  55.  
  56. 1100100
  57. 1001101
  58.  
  59.  
  60. 1110100
  61. 1001110
  62. ОШИБКА!!!
  63. */
  64. int main()
  65. {
  66.     ios::sync_with_stdio(0);
  67.     cin.tie(0);
  68.     cout.tie(0);
  69.     string s,t;
  70.     cin>>s>>t;
  71.     if (s == t){
  72.         cout<<0;
  73.         exit(0);
  74.     }
  75.     int n = s.size();
  76.     vector <ll> powp(n, 1);
  77.     for (int i = 1; i < n; ++i){
  78.         powp[i] = powp[i - 1] * p % mod;
  79.     }
  80.     vector <ll> sf(n), tf(n);
  81.     sf[0] = s[0] - '0' + 1;
  82.     tf[0] = t[0] - '0' + 1;
  83.     for (int i = 1; i < n; ++i){
  84.         sf[i] = sf[i - 1] + ( s[i] - '0' + 1 ) * powp[i] % mod;
  85.         sf[i] %= mod;
  86.         tf[i] = tf[i - 1] + (t[i] - '0' + 1) * powp[i] % mod;
  87.         tf[i] %= mod;
  88.     }
  89.     if (sh){
  90.         cout<<"sf\n"; cvl(sf);
  91.         cout<<"tf\n"; cvl(tf);
  92.         cout<<"powp\n"; cvl(powp);
  93.     }
  94.     int i = -1, mxi=-1;
  95.     if (sh){
  96.         cout<<"i + 1 = "<<i + 1<<"\n";
  97.         cout<<"powp[n - 1 - (i + 1)] = "<<powp[n - 1 - (i + 1)]<<"\n";
  98.         cout<<"sf[i + 1] * powp[n - 1 - (i + 1)] % mod = "<<sf[i + 1] * powp[n - 1 - (i + 1)] % mod<<"\n";
  99.         cout<<"T\n";
  100.         cout<<"n - 1 - (i + 1) = "<<n - 1 - (i + 1)<<"\n";
  101.         cout<<"tf[ n - 1 - (i + 1)] = "<<tf[ n - 1 - (i + 1)]<<"\n";
  102.  
  103.     }
  104.  
  105.     while (i+1 < n-1){
  106.         if (i + 1 < n - 1 && sf[i + 1] * powp[n - 1 - (i + 1)] % mod == (tf[n - 1] - tf[n - 1 - (i + 2)] + mod) % mod){
  107.             mxi=i+1;
  108.         }
  109.         if (sh){
  110.             cout<<"i + 1 = "<<i + 1<<"\n";
  111.             cout<<"powp[n - 1 - (i + 1)] = "<<powp[n - 1 - (i + 1)]<<"\n";
  112.             cout<<"sf[i + 1] * powp[n - 1 - (i + 1)] % mod = "<<sf[i + 1] * powp[n - 1 - (i + 1)] % mod<<"\n";
  113.             cout<<"T\n";
  114.             cout<<"n - 1 - (i + 2) = "<<n - 1 - (i + 2)<<"\n";
  115.             cout<<"tf[ n - 1 - (i + 2)] = "<<tf[ n - 1 - (i + 2)]<<"\n";
  116.  
  117.         }
  118.         i++;
  119.     }
  120.     cout<<n-(mxi+1);
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement