이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define f first
#define s second
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define vec vector
#define pb push_back
#define sz(x) (int)(x).size()
#define pw(x) (1LL<<(x))
#define fast_resp ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
typedef pair<int,ll> pil;
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
const ll inf=1e18;
const int N=2500+3;
ll dp[N][N];
///l,r,prefix
//int pref[N][N];
vec<int> zet[N];
vec<int> z_func(string s){
int l=0,r=-1;
vec<int> zt(sz(s),0);
for(int i=1;i<sz(s);i++){
zt[i]=(i>r?0:min(r-i+1,zt[i-l]));
while(i+zt[i]<sz(s) && s[i+zt[i]]==s[zt[i]]) ++zt[i];
if(i+zt[i]-1>r){
r=i+zt[i]-1;
l=i;
}
}
return zt;
}
signed main(){
fast_resp;
int n;
string s;
cin>>n>>s;
int a,b,c;
cin>>a>>b>>c;
for(int j=0;j<N;j++){
for(int k=0;k<N;k++)
dp[j][k]=inf;
}
vec<vec<int>>nxt(n,vec<int>(n+1,-1));
for(int i=0;i<n;i++){
zet[i]=z_func(s.substr(i,n-i)+'#'+s.substr(i,n-i));
int last=0;
for(int j=i;j<n;j++){
// if((last+1)==1 && i==2){
// i=2;
// cout<<zet[i][j-i+n-i+1]<<endl;
// }
while(last+1<=zet[i][j-i+n-i+1] && (j-i)>=last+1){
++last;
// cout<<"WO "<<i<<' '<<last<<' '<<j<<endl;
nxt[i][last]=j;
}
}
}
for(int l=n-1;l>=0;l--){
for(int r=l;r<n;r++){
if(l==r) dp[l][r]=a+b;
// else{
if(l) umin(dp[l-1][r],dp[l][r]+a);
if(r+1<n) umin(dp[l][r+1],dp[l][r]+a);
ll total=dp[l][r]+c;
// if(l==2 && r==2){
// l=r=2;
// l=2;
// }
/// i'ma b
int j=nxt[l][r-l+1];
int last=r;
// cout<<"L "<<l<<' '<<r<<endl;
while(j!=-1){
// assert(j>last);
// cout<<"HEY "<<j-last-1<<endl;
total+=1ll*(j-last-1)*a;total+=c;
umin(dp[l][j+r-l],total+b);
last=j+r-l;
j=nxt[j][r-l+1];
}
// cout<<endl;
// }
}
}
// cout<<dp[2][8]<<endl;
cout<<dp[0][n-1]-b;
return 0;
}
/*
*/
# | 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... |