#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( 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 ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
37 ms |
62968 KB |
Output isn't correct |
2 |
Correct |
36 ms |
62968 KB |
Output is correct |
3 |
Incorrect |
38 ms |
62972 KB |
Output isn't correct |
4 |
Execution timed out |
1097 ms |
111992 KB |
Time limit exceeded |
5 |
Correct |
127 ms |
113968 KB |
Output is correct |
6 |
Incorrect |
59 ms |
74512 KB |
Output isn't correct |
7 |
Incorrect |
56 ms |
72356 KB |
Output isn't correct |
8 |
Incorrect |
50 ms |
71000 KB |
Output isn't correct |
9 |
Incorrect |
484 ms |
117076 KB |
Output isn't correct |
10 |
Incorrect |
50 ms |
71296 KB |
Output isn't correct |