Submission #547003

#TimeUsernameProblemLanguageResultExecution timeMemory
547003ngraceCopy and Paste 3 (JOI22_copypaste3)C++14
25 / 100
3077 ms283600 KiB
#include <vector> #include <iostream> #include <utility> #include <algorithm> #include <map> #include <set> #include <string> using namespace std; #define v vector #define ll long long #define pii pair<int,int> #define fi first #define se second int main(){ int N; cin>>N; string S; cin>>S; ll A,B,C; cin>>A>>B>>C; map<string, int> subToInd; map<int, set<int>> indToStarts; int subInd=1; for(int i=0; i<N; i++){ for(int s=1; s<=N-i; s++){ string sub; for(int j=i; j<i+s; j++) sub+=S[j]; if(subToInd[sub]==0) subToInd[sub]=subInd++; indToStarts[subToInd[sub]].insert(i); } } v<v<int>> nextOccur(N,v<int>(N+1,-1)); for(int i=N-1; i>=0; i--){ for(int s=1; s<=N-i; s++){ string sub; for(int j=i; j<i+s; j++) sub+=S[j]; int ind=subToInd[sub]; auto next = indToStarts[ind].lower_bound(i+s); if(next!=indToStarts[ind].end()) nextOccur[i][s]=(*next); } } v<v<ll>> dp(N, v<ll>(N,1e17)); for(int s=1; s<=N; s++){ for(int l=N-1; l>=0; l--){ dp[l][l]=A; if(s!=1 && l+s-1<N){ dp[l][l+s-1] = min(dp[l][l+s-1], A+dp[l+1][l+s-1]); dp[l][l+s-1] = min(dp[l][l+s-1], dp[l][l+s-1-1]+A); } int num=1, nex=l; while(true){ nex=nextOccur[nex][s]; num++; if(nex==-1) break; int r=nex+s-1; dp[l][r] = min(dp[l][r], dp[l][l+s-1]+B+C*num+(r-l+1 - num*s)*A); dp[l][r] = min(dp[l][r], A+dp[l+1][r]); dp[l][r] = min(dp[l][r], dp[l][r-1]+A); } } } cout<<dp[0][N-1]<<endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...