#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 = ≜
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
328 ms |
147272 KB |
Output is correct |
5 |
Correct |
44 ms |
3920 KB |
Output is correct |
6 |
Correct |
91 ms |
90892 KB |
Output is correct |
7 |
Correct |
91 ms |
90556 KB |
Output is correct |
8 |
Correct |
85 ms |
90256 KB |
Output is correct |
9 |
Correct |
131 ms |
96188 KB |
Output is correct |
10 |
Correct |
91 ms |
90304 KB |
Output is correct |