Submission #1100410

#TimeUsernameProblemLanguageResultExecution timeMemory
1100410codexistentRima (COCI17_rima)C++14
140 / 140
328 ms147272 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define FOR(i, a, b) for(ll i = a; i <= b; i++) struct Trie{ Trie *nx[26]; ll val, ct; Trie(){ FOR(i, 0, 25) nx[i] = NULL; val = ct = 0; } }; ll n, r = 0; Trie trie; void dfs(Trie *ti){ ll chi = 0, b = -1, b2 = -1; FOR(i, 0, 25){ if(ti->nx[i]){ dfs(ti->nx[i]); if(ti->nx[i]->ct){ chi++; if(b <= ti->nx[i]->val){ b2 = b, b = ti->nx[i]->val; }else if(b2 <= ti->nx[i]->val){ b2 = ti->nx[i]->val; } } } if(!chi){ ti->val = ti->ct; }else{ ti->val = max(ti->val, ti->ct + b + chi - 1); } r = max(r, ti->val); if(chi > 1){ r = max(r, ti->ct + b + b2 + chi - 2); } } } int main(){ cin >> n; FOR(i, 1, n){ string s; cin >> s; reverse(begin(s), end(s)); Trie *ctrie = &trie; for(char c : s){ if(!(ctrie->nx[c - 'a'])){ ctrie->nx[c - 'a'] = new Trie(); } ctrie = ctrie->nx[c - 'a']; } ctrie->ct++; } dfs(&trie); cout << r << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...