Submission #994079

#TimeUsernameProblemLanguageResultExecution timeMemory
994079vjudge1Rima (COCI17_rima)C++17
56 / 140
1077 ms78620 KiB
#include <bits/stdc++.h> using namespace std; const long long mod = 1LL<<36; const int M = 3e6; long long pw[M]; signed main() { ios::sync_with_stdio(0); cin.tie(NULL),cout.tie(NULL); vector<long long> hashes={256,149,369,(long long)1e6+1,7369,8369}; srand(time(NULL)); long long h=hashes[rand()%6]; pw[0]=1; for (int i=1;i<M;i++) pw[i]=pw[i-1]*h%mod; int n; cin>>n; map<int,vector<string>> mp; for (int i=0;i<n;i++) { string s; cin>>s; mp[s.size()].push_back(s); } vector<string> v; for (auto i:mp) for (auto j:i.second) v.push_back(j); reverse(v.begin(),v.end()); map<long long,int> ans; while (!v.empty()) { string s=v.back(); v.pop_back(); long long hs=0,m=s.size(); for (int i=1;i<m;i++) hs=hs*h+s[i],hs%=mod; int mx; if (ans.find(hs)!=ans.end()) mx=ans[hs]+1; else mx=1; vector<long long> wht; long long p; for (int c='a';c<='z';c++) { p=hs; p+=pw[m-1]*c%mod; p%=mod; if (ans.find(p)!=ans.end()) mx=max(mx,ans[p]+1),wht.push_back(p); else if(c==s[0]) wht.push_back(p); } for (long long i:wht) ans[i]=mx; } int res=0; for (auto i:ans) res=max(res,i.second); cout<<res<<endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...