#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int n, sz;
string S[N];
int dp[N * 10][2];
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 sm = 0;
int mx1 = 0;
int mx2 = 0;
for (auto [c, v] : g[i]){
// cout << "going to " << v << " with char " << c << endl;
if (have[v]){
sm += have[v];
if (mx2 < dp[v][0])
mx2 = dp[v][0];
if (mx1 < mx2)
swap(mx1, mx2);
}
}
dp[i][0] = mx1 + have[i];
dp[i][1] = mx1 + mx2 + have[i];
if (sm > 1)
dp[i][1] = have[i] + sm + mx1 + mx2 - 2;
if (sm > 0)
dp[i][0] = have[i] + sm + mx1 - 1;
ans = max(ans, dp[i][1]);
// cout << dp[i][0] << " -- " << dp[i][1] << endl;
}
cout << ans << endl;
}
Compilation message
rima.cpp: In function 'void add(std::string)':
rima.cpp:13:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | for (int i = 0; i < s.size(); i ++){
| ~~^~~~~~~~~~
rima.cpp:26:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for (int j = i; j < s.size(); j ++){
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
133460 KB |
Output is correct |
2 |
Correct |
55 ms |
133456 KB |
Output is correct |
3 |
Correct |
54 ms |
133460 KB |
Output is correct |
4 |
Correct |
240 ms |
150380 KB |
Output is correct |
5 |
Correct |
106 ms |
137676 KB |
Output is correct |
6 |
Correct |
80 ms |
146116 KB |
Output is correct |
7 |
Correct |
83 ms |
145892 KB |
Output is correct |
8 |
Correct |
75 ms |
145612 KB |
Output is correct |
9 |
Correct |
135 ms |
149960 KB |
Output is correct |
10 |
Correct |
77 ms |
145592 KB |
Output is correct |