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<iostream>
#include<stdio.h>
#include<stdint.h>
#include<vector>
#include<algorithm>
#include<utility>
#include<set>
#include<map>
#include<queue>
#include<stack>
using namespace std;
const int64_t INF = (int64_t) 1e18 + 777;
const int64_t mINF = 2500 + 5;
const int64_t MOD = 1e9 + 9;
const int nbit = 11;
const int ndig = 10;
const int nchar = 26;
const int p = 61;
const int D = 4;
int dr[D] = {0, 1, 0, -1};
int dc[D] = {1, 0, -1, 0};
struct Solution
{
int n;
string s;
int64_t a;
int64_t b;
int64_t c;
vector<vector<int> > nx;
Solution() {}
void solve()
{
cin >> n >> s >> a >> b >> c;
map<pair<int, int> , vector<int> > mp;
for(int i = 0; i < n; i++)
{
int tmp = 0;
int pow = 1;
for(int j = i; j <= n; j++)
{
int ch = s[j] - 'a' + 1;
ch = modmul(ch, pow);
tmp = modadd(tmp, ch);
pow = modmul(pow, p);
mp[make_pair(tmp, j - i)].push_back(i);
}
}
nx.resize(n, vector<int>(n, -1));
for(map<pair<int, int>, vector<int> >::iterator it = mp.begin(); it != mp.end(); it++)
{
pair<int, int> pr = (it)->first;
int len = pr.second;
Run(len, (it)->second);
}
vector<vector<int64_t> > dp(n, vector<int64_t>(n, INF));
for(int i = 0; i < n; i++)
{
dp[i][i] = a;
}
for(int64_t len = 0; len < n; len++)
{
for(int64_t i = 0; i < n; i++)
{
int64_t j = i + len;
if(len == 0 || j >= n) break;
dp[i][j] = min(dp[i][j], dp[i][j - 1] + a);
if(i != n - 1) dp[i][j] = min(dp[i][j], dp[i + 1][j] + a);
}
for(int64_t i = 0; i < n; i++)
{
int64_t l = i;
int64_t j = i + len;
int64_t cnt = 2;
while(nx[l][len] != -1)
{
int64_t nl = nx[l][len];
int64_t nr = nl + len;
int64_t x = nr - i + 1 - cnt * (len + 1);
dp[i][nr] = min(dp[i][nr], dp[i][j] + b + c * cnt + a * x);
cnt++;
l = nl;
}
}
}
// for(int i = 0; i < n; i++)
// {
// for(int j = 0; j < n; j++)
// {
// if(dp[i][j] == INF) cout << "I ";
// else cout << dp[i][j] << " ";
// }
// cout << "\n";
// }
cout << dp[0][n - 1];
}
void Run(int len, vector<int>& pos)
{
int k = pos.size();
int l = k - 2;
int r = k - 1;
while(l >= 0)
{
while(r - 1 >= 0 && pos[r - 1] > pos[l] + len) r--;
if(pos[r] > pos[l] + len) nx[pos[l]][len] = pos[r];
l--;
}
}
int modsub(int t1, int t2)
{
int res = t1 - t2;
if(res < 0) res += MOD;
return res;
}
int modadd(int t1, int t2)
{
int res = t1 + t2;
if(res >= MOD) res -= MOD;
return res;
}
int modmul(int t1, int t2)
{
int64_t res = 1LL * t1 * t2;
return (res % MOD);
}
bool BIT(int& mask, int& j)
{
return (mask & MASK(j));
}
int MASK(int j)
{
return (1 << j);
}
};
void __startup()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
}
int main()
{
__startup();
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
Solution().solve();
}
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... |