/**
* 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);
vector<int> old_dp(n);
for (int i = 0; i < n; i++) {
int ptr = i;
while (ptr + 1 < n && f0[ptr + 1] == f0[i]) {
ptr += 1;
}
if (i == ptr) {
dp[f1[i]] = max(dp[f1[i]], dp[f0[i]] + 1);
continue;
}
int cnt = ptr - i + 1;
for (int j = i; j <= ptr; j++) {
old_dp[j] = dp[f0[j]];
dp[f1[j]] = max(dp[f1[j]], dp[f0[j]] + 1);
}
int mx = 0;
for (int j = i; j <= ptr; j++) {
dp[f1[j]] = max(dp[f1[j]], mx + cnt);
mx = max(mx, old_dp[j]);
}
mx = 0;
for (int j = ptr; j >= i; j--) {
dp[f1[j]] = max(dp[f1[j]], mx + cnt);
mx = max(mx, old_dp[j]);
}
i = ptr;
}
cout << *max_element(dp.begin(), dp.end()) << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
373 ms |
106104 KB |
Output isn't correct |
5 |
Correct |
22 ms |
3796 KB |
Output is correct |
6 |
Incorrect |
39 ms |
42580 KB |
Output isn't correct |
7 |
Incorrect |
34 ms |
42272 KB |
Output isn't correct |
8 |
Incorrect |
33 ms |
42120 KB |
Output isn't correct |
9 |
Incorrect |
71 ms |
47408 KB |
Output isn't correct |
10 |
Incorrect |
38 ms |
42136 KB |
Output isn't correct |