이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
typedef long long llong;
const int MAXN = 2500 + 10;
const int INF = 1e9;
int n;
char s[MAXN];
llong a, b, c;
llong dp[MAXN][MAXN];
bool bl[MAXN][MAXN];
llong f(int l, int r)
{
// std::cout << "call: " << l << ' ' << r << '\n' << std::flush;
if (l == r)
{
return a;
}
if (bl[l][r])
{
return dp[l][r];
}
bl[l][r] = true;
dp[l][r] = std::min(f(l + 1, r) + a, f(l, r - 1) + a);
int kmpF[MAXN];
kmpF[l] = 0;
for (int prefix = l ; prefix < r ; ++prefix)
{
if (prefix != l)
{
kmpF[prefix] = kmpF[prefix - 1];
while (kmpF[prefix] > 0 && s[l + kmpF[prefix]] != s[prefix])
{
// std::cout << "here22: " << prefix << ' ' << kmpF[prefix] << '\n';
kmpF[prefix] = kmpF[l + kmpF[prefix] - 1];
}
if (s[l + kmpF[prefix]] == s[prefix])
{
kmpF[prefix]++;
// std::cout << "incr: " << prefix << ' ' << kmpF[prefix] << '\n';
}
}
int ptr = 0;
llong cnt = 1;
llong cost = f(l, prefix) + b;
for (int j = prefix + 1 ; j <= r ; ++j)
{
if (s[j] == s[l + ptr])
{
ptr++;
if (ptr == prefix - l + 1)
{
cnt++;
ptr = 0;
}
} else
{
while (ptr > 0 && s[l + ptr] != s[j])
{
ptr = kmpF[l + ptr - 1];
}
if (s[j] == s[l + ptr])
{
ptr++;
}
}
}
dp[l][r] = std::min(dp[l][r], cost + cnt * c + (r - l + 1) * a - (prefix - l + 1) * cnt * a);
}
return dp[l][r];
}
void solve()
{
std::cout << f(1, n) << '\n';
}
void input()
{
std::cin >> n;
std::cin >> s + 1;
std::cin >> a >> b >> c;
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
copypaste3.cpp: In function 'void input()':
copypaste3.cpp:94:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
94 | std::cin >> s + 1;
| ~~^~~
# | 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... |