이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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=0; l<N; l++){
int r=l+s-1;
if(r>=N) break;
if(l==r){
dp[l][r]=A;
continue;
}
dp[l][r] = min(dp[l][r], A+dp[l+1][r]);
dp[l][r] = min(dp[l][r], dp[l][r-1]+A);
//only need to account for copying sub strings starting at l, any others are accounted for above
for(int k=1; k<s; k++){
ll num=1, nex=nextOccur[l][k];
while(nex!=-1 && nex+k-1<=r){
num++;
nex=nextOccur[nex][k];
}
dp[l][r] = min(dp[l][r], A*(s-num*k)+num*C+dp[l][l+k-1]+B);
}
}
}
/**for(int l=0; l<N; l++){
for(int r=l; r<N; r++){
cout<<l<<" "<<r<<" "<<dp[l][r]<<endl;
}
}**/
cout<<dp[0][N-1]<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |