#include <bits/stdc++.h>
#define inf 2e9
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
const int N = 500000 + 3;
int n;
string s[N];
struct node{
int ed, dp[2];
node *next[26];
node(){
ed = dp[0] = dp[1] = 0;
for (int i = 0; i < 26; i++) next[i] = nullptr;
}
};
node *root = new node();
void add(string &s){
node *cur = root;
for (auto u: s){
if (cur->next[u - 'a'] == nullptr)
cur->next[u - 'a'] = new node;
cur = cur->next[u - 'a'];
}
cur->ed++;
}
int ans = 0;
void dfs(node *cur){
for (int i = 0; i < 26; i++){
if (cur->next[i] == nullptr) continue;
dfs(cur->next[i]);
if (cur->ed) cur->dp[0] += cur->next[i]->dp[0];
else cur->dp[1] += cur->next[i]->dp[0];
}
cur->dp[0] += cur->ed;
ans = max(ans, cur->dp[0] + cur->dp[1]);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s[i];
reverse(all(s[i]));
add(s[i]);
}
dfs(root);
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
16000 KB |
Output is correct |
2 |
Correct |
14 ms |
16000 KB |
Output is correct |
3 |
Correct |
13 ms |
16128 KB |
Output is correct |
4 |
Incorrect |
362 ms |
162840 KB |
Output isn't correct |
5 |
Correct |
31 ms |
22400 KB |
Output is correct |
6 |
Incorrect |
83 ms |
97812 KB |
Output isn't correct |
7 |
Incorrect |
92 ms |
97412 KB |
Output isn't correct |
8 |
Incorrect |
81 ms |
97112 KB |
Output isn't correct |
9 |
Incorrect |
115 ms |
105680 KB |
Output isn't correct |
10 |
Incorrect |
88 ms |
97152 KB |
Output isn't correct |