#include<bits/stdc++.h>
using namespace std ;
const int N = 5e5 + 7 ;
int n ;
int dp[N] ;
int ans ;
string ss[N] ;
struct hashing {
long long base ;
long long mod1;
vector<long long> pref;
vector<long long> base_power;
long long normalize(long long x ){
return ((x%mod1) + mod1) %mod1 ;
}
string s ;
void init(string &str_,long long mod1_ = 1e9 +7, long long base_ = 313){
int sz = str_.size() ;
pref.resize(sz) ;
base_power.resize(sz) ;
base = base_ ;
mod1 = mod1_ ;
s = str_ ;
base_power[0] = 1 ;
for(int i = 1 ;i <sz ; i++){
base_power[i] = (1ll*base*base_power[i-1]) %mod1 ;
}
long long cur = 0 ;
for(int i = 0 ; i <sz ; i++){
cur = (cur*base + str_[i])%mod1;
pref[i] = cur ;
}
}
long long get(int l , int r){
r-- ;
long long ret = pref[r] ;
if(l){
long long power_difference = base_power[r-l+1];
long long prefix_hash =pref[l-1];
prefix_hash = (prefix_hash*power_difference)%mod1 ;
ret = normalize(ret - prefix_hash) ;
}
return ret ;
}
} shs[N] ;
bool match(int i , int j){
if(ss[i] .size() > ss[j].size()) return match(j , i) ;
if( abs( (int) ss[i].size() - (int) ss[j].size()) > 1 ) return 0 ;
if(shs[i].get(0 , ss[i].size() ) == shs[j].get(0 , ss[j].size())) return 1 ;
int k = ss[j] .size() - 1 ;
return (shs[i].get(0 , k) == shs[j].get(0 , k ) );
}
int main(){
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
//freopen("in.in" , "r" , stdin) ;
cin>>n ;
for(int i = 0 ; i < n ;i++){
cin>>ss[i] ;
reverse( ss[i] .begin() , ss[i].end()) ;
}
sort(ss , ss + n) ;
for(int i = 0 ; i < n; i++){
shs[i].init(ss[i]) ;
}
for(int i = 0 ;i < n ;i++){
dp[i] = 1 ;
for(int j = 0 ; j < i ; j++){
if(match(j , i)){
dp[i] = max(dp[i] , dp[j]+1) ;
}
}
ans = max(ans , dp[i]) ;
}
cout<< ans ;
return 0 ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
62968 KB |
Output isn't correct |
2 |
Correct |
36 ms |
62968 KB |
Output is correct |
3 |
Incorrect |
36 ms |
62968 KB |
Output isn't correct |
4 |
Execution timed out |
1104 ms |
109480 KB |
Time limit exceeded |
5 |
Correct |
127 ms |
113784 KB |
Output is correct |
6 |
Incorrect |
60 ms |
74256 KB |
Output isn't correct |
7 |
Incorrect |
54 ms |
72092 KB |
Output isn't correct |
8 |
Incorrect |
51 ms |
70740 KB |
Output isn't correct |
9 |
Incorrect |
594 ms |
116948 KB |
Output isn't correct |
10 |
Incorrect |
50 ms |
71036 KB |
Output isn't correct |