/**
* author: wxhtzdy
* created: 17.07.2023 21:12:57
**/
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<string> s(n);
for (int i = 0; i < n; i++) {
cin >> s[i];
reverse(s[i].begin(), s[i].end());
}
sort(s.begin(), s.end(), [&](string s, string t) {
if (s.size() != t.size()) {
return s.size() < t.size();
} else {
return s < t;
}
});
const int A = 26;
vector<vector<int>> trie(1, vector<int>(A, -1));
vector<int> f0(n), f1(n);
for (int i = 0; i < n; i++) {
int nd = 0;
for (int j = 0; j < (int) s[i].size(); j++) {
int x = (int) (s[i][j] - 'a');
if (trie[nd][x] == -1) {
trie[nd][x] = (int) trie.size();
trie.push_back(vector<int>(A, -1));
}
nd = trie[nd][x];
if (j == (int) s[i].size() - 2) {
f0[i] = nd;
}
if (j == (int) s[i].size() - 1) {
f1[i] = nd;
}
}
}
int sz = (int) trie.size();
vector<int> dp(sz);
for (int i = 0; i < n; i++) {
int ptr = i;
while (ptr + 1 < n && f0[ptr + 1] == f0[i]) {
ptr += 1;
}
int cnt = ptr - i + 1;
int ndp = dp[f0[i]] + cnt;
for (int j = i; j <= ptr; j++) {
dp[f1[j]] = max(dp[f1[j]], ndp);
}
i = ptr;
}
cout << *max_element(dp.begin(), dp.end()) << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
345 ms |
106528 KB |
Output isn't correct |
5 |
Correct |
23 ms |
3748 KB |
Output is correct |
6 |
Incorrect |
42 ms |
42588 KB |
Output isn't correct |
7 |
Incorrect |
33 ms |
42376 KB |
Output isn't correct |
8 |
Incorrect |
34 ms |
42048 KB |
Output isn't correct |
9 |
Incorrect |
72 ms |
47344 KB |
Output isn't correct |
10 |
Incorrect |
34 ms |
42120 KB |
Output isn't correct |