# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
994109 | vjudge1 | Rima (COCI17_rima) | C++17 | 241 ms | 148672 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int n, sz;
string S[N];
int dp[N * 10];
bool have[N * 10];
vector<pair<char, int>> g[N * 10];
void add(string s){
int cur = 0;
for (int i = 0; i < s.size(); i ++){
int nxt = 0;
for (auto [c, v] : g[cur]){
if (c == s[i]){
nxt = v;
break;
}
}
if (nxt){
cur = nxt;
}
else{
for (int j = i; j < s.size(); j ++){
sz++;
g[cur].push_back({s[j], sz});
cur = sz;
}
break;
}
}
have[cur] = 1;
}
void print(){
for (int i = 0; i <= sz; i ++){
cout << "current node = " << i << endl;
for (auto [c, v] : g[i]){
cout << "child " << v << " with char " << c << endl;
}
}
}
int main(){
cin >> n;
for (int i = 0; i < n; i ++){
cin >> S[i];
reverse(S[i].begin(), S[i].end());
add(S[i]);
}
int ans = 0;
// print();
// cout << endl;
for (int i = sz; i >= 0; i --){
// cout << "current node = " << i << endl;
int mx1 = 0;
int mx2 = 0;
for (auto [c, v] : g[i]){
// cout << "going to " << v << " with char " << c << endl;
if (have[v]){
if (mx2 < dp[v])
mx2 = dp[v];
if (mx1 < mx2)
swap(mx1, mx2);
}
}
dp[i] = mx1 + mx2 + have[i];
ans = max(ans, dp[i]);
// cout << "Answer = " << dp[i] << endl;
}
cout << ans << endl;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |