#include<bits/stdc++.h>
using namespace std;
// define
//bool M1;
//#define execute cerr << " Time: " << fixed << setprecision(6) << (1.0 * clock() / CLOCKS_PER_SEC) << "s\n";
//#define memory cerr << abs(&M2 - &M1)/1024.0/1024 << " MB" << "\n"
#define ll long long
#define ii pair <int , int>
#define iii pair <int , ii>
#define se second
#define fi first
#define all(v) (v).begin() , (v).end()
#define Unique(v) sort(all(v)) , v.resize(unique(all(v)) - v.begin())
#define bit(x,i) (((x) >> (i)) & 1LL)
#define flip(x,i) ((x) ^ (1LL << (i)))
#define ms(d,x) memset(d , x , sizeof(d))
#define exist __exist
#define ends __ends
#define visit visited
#define left __left
#define right __right
#define prev __prev
#define next __next
#define sitingfake 1
#define orz 1
//constant
const long long mod = 1e9 + 22771997;
const ll mod2 = 1e9 + 7;
const long long linf = 4557430888798830399LL;
const long long nlinf = -4485090715960753727LL;
const int inf = 1061109567;
const int ninf = -1044266559;
const int dx[] = {0 , -1 , 0 , 1};
const int dy[] = {-1 , 0 , 1 , 0};
template<typename T> bool maximize(T &a, const T &b)
{
if(a < b) {a = b; return 1;}
return 0;
}
template<typename T> bool minimize(T &a, const T &b)
{
if(a > b) {a = b; return 1;}
return 0;
}
void Plus(ll & a ,ll b)
{
b %= mod;
a += b;
if(a < 0) a += mod;
a %= mod;
return;
}
void Mul(ll & a, ll b)
{
(a *= (b % mod)) %= mod;
return;
}
//code
const int maxn = 2600;
int n;
string s;
ll A , B , C;
ll dp[maxn][maxn];
map <int , vector <ii>> mp;
int next[maxn][maxn];
ll Pow[maxn] , H[maxn] , Pow2[maxn] , H2[maxn];
void init()
{
Pow[0] = 1;
Pow2[0] = 1;
for(int i = 1; i <= n; i++)
{
Pow[i] = (Pow[i - 1] * 366LL) % mod;
Pow2[i] = (Pow2[i - 1] * 289LL) % mod2;
}
for(int i = 1; i <= n; i++)
{
H[i] = (H[i - 1] * 366LL + s[i]) % mod;
H2[i] = (H2[i - 1] * 289LL + s[i]) % mod2;
}
}
pair <ll , ll> get(int l , int r)
{
return {(H[r] - H[l - 1] * Pow[r - l + 1] + mod * mod) % mod , (H2[r] - H2[l - 1] * Pow2[r - l + 1] + mod2 * mod2) % mod2};
}
void solve(void)
{
cin >> n;
cin >> s;
cin >> A >> B >> C;
s = ' ' + s;
init();
for(int len = 1; len <= n; len++)
{
map <pair <ll , ll> , int> last;
for(int l = 1; l + len - 1 <= n; l++)
{
int r = l + len - 1;
if(l - len >= 1) last[get(l - len , l - 1)] = l - len;
pair <ll , ll> curHash = get(l , r);
if(last.count(curHash))
{
next[l][r] = last[curHash];
}
}
}
ms(dp , 0x3f);
for(int i = 1; i <= n; i++)
{
dp[i][i] = A;
}
for(int len = 1; len < n; len++)
{
for(int l = 1; l + len - 1 <= n; l++)
{
int r = l + len - 1;
minimize(dp[l][r + 1] , dp[l][r] + A);
minimize(dp[l - 1][r] , dp[l][r] + A);
int j = next[l][r];
int cnt = 1;
while(j)
{
++cnt;
minimize(dp[j][r] , dp[l][r] + B + 1ll * cnt * C + 1ll * (r - j + 1 - cnt * len) * A);
j = next[j][j + len - 1];
}
}
}
cout << dp[1][n];
}
/**
**/
//bool M2;
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define task ""
if(fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
int tc = 1;
// cin >> tc;
while(tc--) solve();
// execute;
// memory;
}
컴파일 시 표준 에러 (stderr) 메시지
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:167:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
167 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
copypaste3.cpp:168:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
168 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~| # | 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... |