Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***
- created: 2022-08-11-01.05.04
- ***/
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- using namespace std;
- using namespace __gnu_pbds;
- template<typename U> using ordered_set=tree<U, null_type,less<U>,rb_tree_tag,tree_order_statistics_node_update>;
- #define ll long long
- #define ull unsigned long long
- #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
- #define get_lost_idiot return 0
- #define nl '\n'
- ull p[1000005];
- ull hash1[8][1000005];
- ull base=307;
- void power()
- {
- p[0]=1;
- for(int i=1; i<=1000003; i++)
- {
- p[i]=p[i-1]*base;
- }
- }
- void hashing(string a,ll in)
- {
- hash1[in][0]=0;
- for(int i=1; i<=a.size(); i++)
- {
- hash1[in][i]=(hash1[in][i-1]*base+a[i-1]);
- }
- }
- ull forwardhash(ll l,ll r,ll in)
- {
- return (hash1[in][r] - (hash1[in][l-1] * p[r-l+1]));
- }
- int main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- power();
- test
- {
- ll n,m,i,j,k,lo=1,hi=1e8,ans=0;
- cin>>n;
- vector<string>a(n+1);
- for(i=0; i<n; i++)
- {
- cin>>a[i];
- hashing(a[i],i);
- hi=min(hi,(ll)a[i].size());
- }
- while(lo<=hi)
- {
- ll mid=(lo+hi)>>1;
- unordered_set<ull>st;
- for(i=0; (i+mid-1)<a[0].size(); i++)
- {
- st.insert(forwardhash(i,i+mid-1,0));
- }
- for(i=1; i<n; i++)
- {
- unordered_set<ull>cur;
- for(j=0; (j+mid-1)<a[i].size(); j++)
- {
- ull h1= forwardhash(j,j+mid-1,i);
- if(st.find(h1)!=st.end())
- {
- cur.insert(h1);
- }
- }
- st=cur;
- }
- if(st.size())
- {
- lo=mid+1;
- ans=mid;
- }
- else hi=mid-1;
- }
- cout<<ans<<nl;
- }
- get_lost_idiot;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement