/**
____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|
**/
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 2510;
const ll base = 29, inf = 1e18;
int n;
ll a, b, c, h[maxn], pw[maxn];
string s;
unordered_map < ll, vector < int > > occ;
unordered_map < ll, ll > hs_dp;
ll get_hash(int i, int j)
{
if (i > j)
return 0;
return h[j] - h[i - 1] * pw[j - i + 1];
}
void solve()
{
cin >> n >> s >> a >> b >> c;
s = "#" + s;
pw[0] = 1;
for (int i = 1; i <= n; i ++)
{
h[i] = h[i - 1] * base + (s[i] - 'a' + 1);
pw[i] = pw[i - 1] * base;
}
for (int i = n; i > 0; i --)
{
for (int p = i; p <= n; p ++)
{
ll cur_hs = get_hash(i, p);
occ[cur_hs].push_back(i);
/**cout << "hash " << i << " " << p << endl;
cout << "occurrences: ";
for (int ds : occ[cur_hs])
cout << ds << " ";
cout << endl;*/
if (occ[cur_hs].size() == 1)
{
hs_dp[cur_hs] = hs_dp[get_hash(i + 1, p)] + a;
}
else
{
hs_dp[cur_hs] = min(hs_dp[cur_hs], hs_dp[get_hash(i + 1, p)] + a);
}
}
for (int k = i; k <= n; k ++)
{
ll clip_hash, clip_len = k - i + 1;
clip_hash = h[k] - h[i - 1] * pw[clip_len];
ll cost = hs_dp[clip_hash] + b;
if (occ[get_hash(i, k)].size() < 2)
break;
int pos = i, pt = (int)(occ[clip_hash].size()) - 1;
///cout << "-------------------" << endl;
///cout << i << " " << k << endl;
while(pos <= n)
{
///cout << pos << " " << cost << endl;
while(pt >= 0 && occ[clip_hash][pt] < pos)
pt --;
if (pt == -1)
break;
///cout << "stop " << occ[clip_hash][pt] << endl;
cost = cost + (occ[clip_hash][pt] - pos) * a;
cost = cost + c;
pos = (occ[clip_hash][pt] + clip_len - 1);
hs_dp[get_hash(i, pos)] = min(hs_dp[get_hash(i, pos)], cost);
pos ++;
/**int to = pos + clip_len - 1;
if (to > n)
{
break;
}
if (get_hash(pos, to) == clip_hash)
{
cost = cost + c;
pos = pos + clip_len;
hs_dp[get_hash(i, to)] = min(hs_dp[get_hash(i, to)], cost);
}
else
{
pos ++;
cost += a;
}*/
}
}
for (int k = i; k <= n; k ++)
{
hs_dp[get_hash(i, k)] = min(hs_dp[get_hash(i, k)], hs_dp[get_hash(i, k - 1)] + a);
}
}
for (int i = 1; i <= n; i ++, cout << endl)
for (int j = i; j <= n; j ++)
cout << hs_dp[get_hash(i, j)] << " ";
cout << hs_dp[h[n]] << endl;
}
int main()
{
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |