# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
785930 | MilosMilutinovic | Kralj (COCI16_kralj) | C++14 | 283 ms | 23392 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/**
* 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 |
---|---|---|---|---|
Fetching results... |