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 = 1e18 + 777;
const int64_t mINF = 2500 + 1;
const int64_t MOD = 1e9 + 9;
const int nbit = 11;
const int ndig = 10;
const int nchar = 26;
const int p = 31;
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<int> pow;
vector<int> inv;
vector<int> prefix;
vector<vector<int> > nx;
Solution() {}
void solve()
{
pow.resize(mINF, 1);
inv.resize(mINF, 1);
for(int i = 2; i < mINF; i++)
{
pow[i] = modmul(pow[i - 1], p);
}
inv[mINF - 1] = CalcInv(pow[mINF - 1]);
for(int i = mINF - 1; i > 1; i--)
{
inv[i - 1] = modmul(inv[i], p);
}
cin >> n >> s >> a >> b >> c;
prefix.resize(n + 1, 0);
for(int i = 1; i <= n; i++)
{
int ch = s[i - 1] - 'a' + 1;
ch = modmul(ch, pow[i]);
ch = modadd(ch, prefix[i - 1]);
prefix[i] = modadd(prefix[i], ch);
}
map<pair<int, int> , vector<int> > mp;
for(int i = 1; i <= n; i++)
{
for(int j = i; j <= n; j++)
{
int x = CalcHash(i, j);
mp[make_pair(x, j - i)].push_back(i - 1);
}
}
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(int len = 0; len < n; len++)
{
for(int i = 0; i < n; i++)
{
int 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(int i = 0; i < n; i++)
{
int64_t l = i;
bool f = true;
while(nx[l][len] != -1)
{
int64_t r = l + len;
int64_t nl = nx[l][len];
int64_t nr = nl + len;
int64_t tmp = dp[i][r] + c + a * (nl - r - 1);
if(f) tmp += b + c;
// cerr << "i = " << i << " ; r = " << r << " ; nl - r - 1 = " << nl - r - 1 << "\n";
// cerr << "dp = " << dp[i][r] << " ; tmp = " << tmp << "\n";
dp[i][nr] = min(dp[i][nr], tmp);
f = false;
l = nl;
}
}
}
// for(int i = 0; i < n; i++)
// {
// for(int j = 0; j < n; j++)
// {
// cout << dp[i][j] << " \n"[j == n - 1];
// }
// }
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 CalcHash(int l, int r)
{
int ans = modsub(prefix[r], prefix[l - 1]);
ans = modmul(ans, inv[l]);
return ans;
}
int CalcInv(int t1)
{
int res = 1;
int t2 = MOD - 2;
while(t2 != 0)
{
if(t2 & 1) res = modmul(res, t1);
t1 = modmul(t1, t1);
t2 >>= 1;
}
return res;
}
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... |