Advertisement
Saleh127

UVA 1227 / Hashing

Aug 10th, 2022
826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. /***
  2.  created: 2022-08-11-01.05.04
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8. using namespace std;
  9. using namespace __gnu_pbds;
  10. template<typename U> using ordered_set=tree<U, null_type,less<U>,rb_tree_tag,tree_order_statistics_node_update>;
  11. #define ll long long
  12. #define ull unsigned long long
  13. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  14. #define get_lost_idiot return 0
  15. #define nl '\n'
  16.  
  17. ull p[1000005];
  18. ull hash1[8][1000005];
  19. ull base=307;
  20.  
  21. void power()
  22. {
  23.     p[0]=1;
  24.     for(int i=1; i<=1000003; i++)
  25.     {
  26.         p[i]=p[i-1]*base;
  27.     }
  28. }
  29.  
  30. void hashing(string a,ll in)
  31. {
  32.     hash1[in][0]=0;
  33.     for(int i=1; i<=a.size(); i++)
  34.     {
  35.         hash1[in][i]=(hash1[in][i-1]*base+a[i-1]);
  36.     }
  37. }
  38.  
  39. ull forwardhash(ll l,ll r,ll in)
  40. {
  41.     return (hash1[in][r] - (hash1[in][l-1] * p[r-l+1]));
  42. }
  43.  
  44.  
  45. int main()
  46. {
  47.     ios_base::sync_with_stdio(0);
  48.     cin.tie(0);
  49.     cout.tie(0);
  50.  
  51.     power();
  52.  
  53.     test
  54.     {
  55.         ll n,m,i,j,k,lo=1,hi=1e8,ans=0;
  56.  
  57.         cin>>n;
  58.  
  59.         vector<string>a(n+1);
  60.  
  61.         for(i=0; i<n; i++)
  62.         {
  63.             cin>>a[i];
  64.             hashing(a[i],i);
  65.             hi=min(hi,(ll)a[i].size());
  66.         }
  67.  
  68.  
  69.         while(lo<=hi)
  70.         {
  71.             ll mid=(lo+hi)>>1;
  72.  
  73.             unordered_set<ull>st;
  74.  
  75.             for(i=0; (i+mid-1)<a[0].size(); i++)
  76.             {
  77.                 st.insert(forwardhash(i,i+mid-1,0));
  78.             }
  79.  
  80.             for(i=1; i<n; i++)
  81.             {
  82.                 unordered_set<ull>cur;
  83.  
  84.                 for(j=0; (j+mid-1)<a[i].size(); j++)
  85.                 {
  86.                     ull h1= forwardhash(j,j+mid-1,i);
  87.  
  88.                     if(st.find(h1)!=st.end())
  89.                     {
  90.                         cur.insert(h1);
  91.                     }
  92.  
  93.                 }
  94.  
  95.                 st=cur;
  96.             }
  97.  
  98.             if(st.size())
  99.             {
  100.                 lo=mid+1;
  101.                 ans=mid;
  102.             }
  103.             else hi=mid-1;
  104.         }
  105.  
  106.         cout<<ans<<nl;
  107.  
  108.     }
  109.  
  110.     get_lost_idiot;
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement