#include <bits/stdc++.h>
using namespace std;
#define task "main"
#define F first
#define S second
#define ii pair<int, int>
#define il pair<int, long long>
#define li pair<long long, int>
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
const int MAX_N = (int)5e5 + 5;
const int MAX_LEN = (int)3e6 + 6;
int nWord;
string word;
struct NODE {
int child[26];
int dp = 0;
bool isEnd = 0;
};
vector<NODE> trie;
NODE newNode;
int nNode = 0;
void addWord(string &s) {
int id = 0;
for (char ch : s) {
int c = ch - 'a';
if (!trie[id].child[c]) {
trie[id].child[c] = ++nNode;
trie.push_back(newNode);
}
id = trie[id].child[c];
}
trie[id].isEnd = 1;
}
void updateMax(ii &a, int x) {
if (a.F < x) a.S = a.F, a.F = x;
else if (a.S < x) a.S = x;
}
int ans = 0;
void calc(int u) {
int cntChild = 0;
ii maxChilds = {0, 0};
FOR(c, 0, 25) {
int childID = trie[u].child[c];
if (!childID) continue;
calc(childID);
cntChild += trie[childID].isEnd;
updateMax(maxChilds, trie[childID].dp);
maximize(trie[u].dp, trie[childID].dp);
}
if (trie[u].isEnd) trie[u].dp += 1 + max(0, cntChild - 1);
else trie[u].dp = 0;
maximize(ans, maxChilds.F + maxChilds.S + trie[u].isEnd + max(0, cntChild - 2));
}
void solve() {
trie.push_back(newNode);
cin >> nWord;
FOR(i, 1, nWord) {
cin >> word;
reverse(word.begin(), word.end());
addWord(word);
}
calc(0);
cout << ans;
}
int32_t main() {
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
bool multitest = 0;
int numTest = 1;
if (multitest) cin >> numTest;
while (numTest--) {
solve();
}
return 0;
}
/* Lak lu theo dieu nhac!!!! */
컴파일 시 표준 에러 (stderr) 메시지
rima.cpp: In function 'int32_t main()':
rima.cpp:99:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
rima.cpp:100:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |