#include <bits/stdc++.h>
using namespace std;
#define fileio() freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define fio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define allr(x) x.rbegin(), x.rend()
#define cmprs(x) sort(all(x)),x.erase(unique(all(x)),x.end())
#define endl "\n"
#define sp " "
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define rz resize
#define sz(a) (int)(a.size())
#define clr(a) a.clear()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef tuple<int, int, int> tpi;
typedef tuple<ll, ll, ll> tpl;
typedef pair<double, ll> pdl;
typedef pair<double, int> pdi;
const int dx[] = {1,-1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
const ll MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MAX = 2525; // PLZ CHK!
template <ll P, ll M> struct Hash {
vector<ll> H,B;
void build(const string &S) {
H.resize(sz(S)+1), B.resize(sz(S)+1);
B[0]=1;
for (int i=1; i<=sz(S); i++) H[i]=(H[i-1]*P+S[i-1])%M;
for (int i=1; i<=sz(S); i++) B[i]=B[i-1]*P%M;
}
ll get(int s, int e) {
return (H[e]-H[s-1]*B[e-s+1]%M+M)%M;
}
};
Hash<1299709, 1'000'000'007> H;
int N;
ll A,B,C,D[MAX][MAX],I[MAX][MAX];
string S;
int main() {
fio();
cin>>N>>S>>A>>B>>C;
H.build(S);
S=' '+S;
map<ll,vector<pii>> idx;
for (int i=1; i<=N; i++) {
for (int j=i; j<=N; j++) {
idx[H.get(i,j)].pb({i,j});
}
}
fill_n(&I[0][0],MAX*MAX,LINF);
for (auto &[t,v]:idx) {
for (int i=0; i<sz(v); i++) {
auto [s,e]=v[i];
int t=lb(all(v),pii{e+1,-1})-v.begin();
if (t<sz(v)) I[s][e]=v[t].first;
}
}
fill_n(&D[0][0],MAX*MAX,LINF);
for (int i=1; i<=N; i++) D[i][i]=A+B;
for (int i=1; i<N; i++) {
for (int l=1,r=l+i-1; r<=N; l++,r++) {
if (l>1) D[l-1][r]=min(D[l-1][r], A+D[l][r]);
if (r<N) D[l][r+1]=min(D[l][r+1], A+D[l][r]);
int cur=I[l][r], cnt=2;
while (cur<N) {
D[l][cur+i-1]=min(D[l][cur+i-1], D[l][r]+C*cnt+A*(cur+i-l-cnt*i)+B);
cnt++,cur=I[cur][cur+i-1];
}
}
}
cout<<D[1][N]-B;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
100176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
100176 KB |
Output is correct |
2 |
Correct |
14 ms |
100176 KB |
Output is correct |
3 |
Correct |
164 ms |
118504 KB |
Output is correct |
4 |
Correct |
195 ms |
120648 KB |
Output is correct |
5 |
Correct |
233 ms |
124232 KB |
Output is correct |
6 |
Correct |
318 ms |
128584 KB |
Output is correct |
7 |
Correct |
468 ms |
134080 KB |
Output is correct |
8 |
Correct |
384 ms |
134088 KB |
Output is correct |
9 |
Incorrect |
382 ms |
133960 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
100176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
100176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
100176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
13 ms |
100176 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |