Submission #229899

#TimeUsernameProblemLanguageResultExecution timeMemory
229899mohamedsobhi777Rima (COCI17_rima)C++14
0 / 140
416 ms60620 KiB
#include<bits/stdc++.h> using namespace std ; const int N = 1e5 + 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 ; } 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_ ; 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){ 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 ; return shs[i].get(0 , ss[i].size() - 1) == shs[j].get(0 , ss[j].size() -1 ) ; } 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()) ; shs[i].init(ss[i]) ; } sort(ss , ss + n) ; 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 timeMemoryGrader output
Fetching results...