#include "bits/stdc++.h"
using namespace std;
#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
// #define int long long
const int MOD = 1e9 + 7;
const int inf = 2e9;
const int N = 3e6 + 7;
struct Trie {
struct Node {
Node * child[26];
int cnt;
Node() {
for(int i = 0; i < 26; i++) child[i] = NULL;
cnt = 0;
}
};
int ans;
Node* root;
Trie(){
root = new Node();
};
void add(string s) {
Node * p = root;
for(char c: s) {
int i = c - 'a';
if(p->child[i] == NULL) p->child[i] = new Node();
p = p->child[i];
}
p->cnt = 1;
}
int dfs(Node * p) {
int ans1 = 0, ans2 = 0;
int sus = p->cnt;
int cnt = 0;
for(int i = 0; i < 26; i++) {
if(p->child[i] == NULL) continue;
Node * v = p->child[i];
int x = dfs(v);
if(v->cnt) cnt++;
if(x >= ans1) {
ans2 = ans1;
ans1 = x;
} else if(x > ans2) {
ans2 = x;
}
}
if(sus) {
sus += cnt;
if(ans1) sus = sus - 1 + ans1;
}
int cur = ans1 + ans2 + cnt;
if(ans1) cur--;
if(ans2) cur--;
ans = max(ans, sus);
ans = max(ans, cur);
return sus;
}
} tri;
signed main() {
cin.tie(0) -> sync_with_stdio(0);
// if(fopen("rima.inp", "r")) {
// freopen("rima.inp", "r", stdin);
// }
int n; cin >> n;
for(int i = 1; i <= n; i++) {
string s; cin >> s;
reverse(all(s));
tri.add(s);
}
tri.dfs(tri.root);
cout << tri.ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
508 KB |
Output is correct |
4 |
Correct |
280 ms |
134836 KB |
Output is correct |
5 |
Correct |
14 ms |
1104 KB |
Output is correct |
6 |
Incorrect |
84 ms |
79784 KB |
Output isn't correct |
7 |
Incorrect |
86 ms |
79608 KB |
Output isn't correct |
8 |
Incorrect |
78 ms |
79448 KB |
Output isn't correct |
9 |
Incorrect |
99 ms |
82516 KB |
Output isn't correct |
10 |
Incorrect |
86 ms |
79500 KB |
Output isn't correct |