Submission #547012

#TimeUsernameProblemLanguageResultExecution timeMemory
547012ngraceCopy and Paste 3 (JOI22_copypaste3)C++14
57 / 100
3074 ms39672 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;

    v<v<int>> nextOccur(N,v<int>(N+1,N));
    for(int l=0; l<N; l++){
        for(int i=l+1; i<N; i++){
            if(S[l]==S[i]){
                nextOccur[l][1]=i;
                break;
            }
        }
    }
    for(int s=2; s<=N; s++){
        for(int l=0; l<=N-s; l++){
            int nex=nextOccur[l][s-1];
            while(nex<=N-s){
                if(S[l+s-1]==S[nex+s-1]){
                    nextOccur[l][s]=nex;
                    break;
                }
                nex=nextOccur[nex][s-1];
            }
        }
    }

    //make next occurence disjoint
    for(int l=0; l<N; l++){
        for(int s=1; s<=N-l; s++){
            int nex=nextOccur[l][s];
            while(nex<N && nex<l+s) nex=nextOccur[nex][s];
            nextOccur[l][s] = nex;
        }
    }
    

    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=2, nex=nextOccur[l][s];
            while(nex<N){
                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);
                nex=nextOccur[nex][s];
                num++;
            }
        }
    }

    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...