# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
229911 | mohamedsobhi777 | Rima (COCI17_rima) | C++14 | 1097 ms | 117076 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 ;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |