# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
475344 | cpp219 | Rima (COCI17_rima) | C++14 | 702 ms | 103736 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
#define debug(y) cout<<y,exit(0)
using namespace std;
typedef pair<ll,ll> LL;
typedef pair<ld,ll> LD;
const ll N = 5e5 + 9;
const ll mod = 982451653;
const ll base = 31;
string s[N];
vector<ll> Hash[N];
ll n,PW[N*6],ans;
unordered_map<ll,ll> val;
bool lf(string a,string b){
return (a.size() < b.size() || a.size() == b.size() && a < b);
}
ll GetHash(ll id,ll L,ll R){
return (Hash[id][R] - Hash[id][L - 1]*PW[R - L + 1] + mod*mod)%mod;
}
ll Prefix1(ll id){
return GetHash(id,1,s[id].size() - 2);
}
ll PrefixAll(ll id){
return Hash[id][s[id].size() - 1];
}
int main(){
ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
#define task "tst"
if (fopen(task".inp","r")){
freopen(task".inp","r",stdin);
//freopen(task".out","w",stdout);
}
PW[0] = 1;
for (ll i = 1;i < N*6;i++) PW[i] = (PW[i - 1] * base)%mod;
cin>>n;
for (ll i = 1;i <= n;i++){
cin>>s[i]; reverse(s[i].begin(),s[i].end());
s[i] = ' ' + s[i];
}
sort(s + 1,s + n + 1,lf);
for (ll i = 1;i <= n;i++){
ll sz = s[i].size() - 1; Hash[i].resize(sz + 1);
for (ll j = 1;j <= sz;j++)
Hash[i][j] = (Hash[i][j - 1] * base + s[i][j] - 'a' + 1)%mod;
}
//for (ll i = 1;i <= n;i++) cout<<s[i]<<"\n"; exit(0);
//cout<<Prefix1(4)<<" "<<GetHash(4,1,3); return 0;
for (ll i = 1;i <= n;i){
//cout<<i<<"\n";
ll j = i + 1,prv = Prefix1(i);
while(j <= n && prv == Prefix1(j) && s[j].size() == s[i].size()) j++;
for (ll l = i;l < j;l++){
ll now = PrefixAll(l);
val[now] = max(val[now],j - i + val[prv]);
ans = max(ans,val[now]);
}
//cout<<i<<" "<<j<<"\n";
i = j; //debug(i);
}
cout<<ans;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |