#include <bits/stdc++.h>
#define dbg(x) cerr << #x << " = " << x << "\n"
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie()
using namespace std;
using ll = long long;
const int N = 5e5 + 10;
int n, ans;
string s[N];
struct trie {
trie *nxt[26];
int cnt, res;
trie() {
for (int i = 0; i < 26; ++i)
nxt[i] = 0;
cnt = 0;
res = 0;
}
void add(string &t, int p = 0) {
if (p == int(t.size())) {
++cnt;
return;
}
int j = t[p] - 'a';
if (nxt[j] == 0)
nxt[j] = new trie();
nxt[j] -> add(t, p + 1);
}
int f(int x) {
vector <int> v;
v.clear();
int total = 0;
for (int i = 0; i < 26; ++i) {
if (nxt[i] == 0)
continue;
int cur = (nxt[i] -> f(i));
total += (nxt[i] -> cnt);
if (nxt[i] -> cnt)
v.push_back(cur);
}
sort(v.rbegin(), v.rend());
int mx = 0;
for (int i = 0; i < min(int(v.size()), 2); ++i)
mx += v[i];
//cerr << char(x + 'a') << " " << (v.empty() ? 0 : v[0]) << " " << mx << " " << total << " " << (this -> cnt) << " " << ans << "\n";
ans = max(ans, mx + total + (this -> cnt));
return (v.empty() ? 0 : v[0]) + total;
}
} tr;
int main() {
fast_io;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> s[i];
reverse(s[i].begin(), s[i].end());
tr.add(s[i]);
}
tr.f(-1);
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
16000 KB |
Output is correct |
2 |
Correct |
14 ms |
16128 KB |
Output is correct |
3 |
Correct |
13 ms |
16000 KB |
Output is correct |
4 |
Correct |
368 ms |
150544 KB |
Output is correct |
5 |
Correct |
30 ms |
19712 KB |
Output is correct |
6 |
Correct |
98 ms |
106468 KB |
Output is correct |
7 |
Correct |
88 ms |
106268 KB |
Output is correct |
8 |
Correct |
99 ms |
106068 KB |
Output is correct |
9 |
Correct |
122 ms |
111956 KB |
Output is correct |
10 |
Correct |
87 ms |
105984 KB |
Output is correct |