#include<bits/stdc++.h>
using namespace std ;
const int N = 5e5 + 7 ;
int n ;
string ss[N] ;
struct trie{
int nxt[N*6][27] ;
int cnt[N] ;
int t = 1 ;
int dp[N*6][2] ;
void add(string &add_ ){
int cur = 1 ;
int sz = add_.size() ;
for(int i = 0 ;i < sz ;i++ ){
int c = add_[i] - 'a' ;
if(!nxt[cur][c])nxt[cur][c] = ++t ;
cur = nxt[cur][c] ;
}
cnt[cur] ++ ;
}
int solve(int i , bool flag){
if(dp[i][flag]!=-1) return dp[i][flag] ;
if(flag && !cnt[i]) return -1e8 ;
int ret = 0 ;
int kids = 0 ;
for(int j = 0 ;j < 26 ;j ++){
kids +=cnt[nxt[i][j]] ;
}
for(int j = 0 ;j < 26 ; j++){
if(nxt[i][j]){
ret = max(ret , solve(nxt[i][j] , flag ) ) ;
ret = max(ret , solve(nxt[i][j] , 1) + kids ) ;
}
}
return dp[i][flag] =ret ;
}
} tr;
int main(){
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
//freopen("in.in" , "r" , stdin) ;
memset(tr.dp , -1 , sizeof tr.dp) ;
cin>>n ;
for(int i = 0 ; i < n ;i++){
string x ;
cin>>x ;
reverse( x .begin() , x.end()) ;
tr.add(x) ;
}
cout<< tr.solve(1 , 0) ;
return 0 ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
39552 KB |
Output is correct |
2 |
Correct |
25 ms |
39544 KB |
Output is correct |
3 |
Correct |
25 ms |
39552 KB |
Output is correct |
4 |
Incorrect |
169 ms |
106408 KB |
Output isn't correct |
5 |
Correct |
44 ms |
40568 KB |
Output is correct |
6 |
Incorrect |
138 ms |
170328 KB |
Output isn't correct |
7 |
Incorrect |
137 ms |
170088 KB |
Output isn't correct |
8 |
Incorrect |
137 ms |
170148 KB |
Output isn't correct |
9 |
Incorrect |
160 ms |
171808 KB |
Output isn't correct |
10 |
Incorrect |
137 ms |
170572 KB |
Output isn't correct |