This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
//#pragma GCC optimize("trapv")
#define st first
#define nd second
#define pb push_back
#define eb emplace_back
#define pp(x) pop_back(x)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define rev(x) reverse(all(x))
#define sor(x) sort(all(x))
#define sz(x) (int)(x).size()
#define rsz(x) resize(x)
using namespace std;
///~~~~~~~~~~~~~~~~~~~~~~~~~~
template <typename H, typename T>
ostream& operator<<(ostream& os, pair<H, T> m){
return os <<"("<< m.st<<", "<<m.nd<<")";
}
template <typename H>
ostream& operator<<(ostream& os, vector<H> V){
os<<"{";
for(int i=0; i<V.size(); i++){
if(i)os<<" ";
os<<V[i];
}
os<<"}";
return os;
}
void debug(){cerr<<"\n";}
template <typename H, typename... T>
void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);}
#define deb(x...) cerr<<#x<<" = ";debug(x);
//#define deb(x...) ;
///~~~~~~~~~~~~~~~~~~~~~~~~~
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii > vii;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef string str;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N=1<<20, INF=1e9+5, mod2=1e9+7, mod=998244353;
struct modint{
int n=0;
modint(){}
modint(ll x){
n=x%mod;
if(n<0)n+=mod;
}
operator int(){
return n;
}
modint operator+(modint a){a.n = n+a.n; if(a.n>=mod)a.n-=mod;return a;}
modint operator+=(modint a){return (*this)=(*this)+a;}
modint operator-(modint a){a.n = n-a.n; if(a.n<0)a.n+=mod;return a;}
modint operator-=(modint a){return (*this)=(*this)-a;}
modint operator*(modint a){a.n = (n*1ll*a.n)%mod; return a;}
modint operator*=(modint a){return (*this)=(*this)*a;}
modint por(const ll &m)const{
modint a(1);
if(m==0)return a;
if(m==1)return (*this);
a=por(m/2);
a*=a;
return a*(por(m&1));
}
modint odw(){
return por((ll)mod-2);
}
modint operator/(modint a){return (*this)*a.odw();}
modint operator/=(modint a){return (*this)=(*this)/a;}
bool operator==(modint a){return a.n==n;}
friend ostream& operator<<(ostream& os, modint m) {
return os << m.n;
}
};
modint fact[N], fact2[N];
typedef vector<modint> vm;
void factinit(){
fact[0]=1;
for(int i=1; i<N; i++){
fact[i]=(fact[i-1]*modint(i))%mod;
}
fact2[N-1]=fact[N-1].odw();
for(int i=N-2; i>=0; i--){
fact2[i]=(fact2[i+1]*modint(i+1))%mod;
}
}
modint npok(int _n, int _k){
return fact[_n]*fact2[_k]*fact2[_n-_k];
}
const int M=2505;
ll dp[M][M];
int nxt[M][M];
int jmp[M][M];
int main(){
//factinit();
BOOST;
int n;
cin>>n;
string s;
cin>>s;
int a, b, c;
cin>>a>>b>>c;
for(int i=0; i<n; i++){
int t=i+1;
while(t<n && s[i]!=s[t]){
t++;
}
nxt[i][i]=jmp[i][i]=t;
for(int j=i; j<n; j++)dp[i][j]=1e18;
}
deb(jmp[0][0], jmp[1][1]);
for(int d=0; d<n; d++){
if(d){
for(int i=0; i<n-d; i++){
int t=nxt[i][i+d-1];
while(t+d<n && s[i+d]!=s[t+d]){
t=nxt[t][t+d-1];
}
if(t+d>=n)t=n;
nxt[i][i+d]=t;
t=jmp[i][i+d-1];
if(t+d>=n)t=n;
while(t+d<n && (s[i+d]!=s[t+d] || t<=i+d)){
t=nxt[t][t+d-1];
}
jmp[i][i+d]=t;
//deb(i, i+d, nxt[i][i+d], jmp[i][i+d]);
}
}
for(int i=0; i<n-d; i++){
dp[i][i+d]=min(dp[i][i+d], dp[i+1][i+d]+a);
dp[i][i+d]=min(dp[i][i+d], dp[i][i+d-1]+a);
int t=jmp[i][i+d];
for(int j=2; t+d<n; j++, t=jmp[t][t+d]){
//deb(t, jmp[t][t+d], j, (t+d-i+1-(d+1)*j),dp[i][i+d],b,j*c);
dp[i][t+d]=min(dp[i][t+d], a*1ll*(t+d-i+1-(d+1)*j)+dp[i][i+d]+b+j*c);
}
}
}
cout<<dp[0][n-1]<<"\n";
}
# | 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... |