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 <bits/stdc++.h>
using namespace std;
#define ll long long
const ll nx=505, mod=1e9+7, mul=31;
ll n, a, b, c, s[nx], dp[nx][nx];
string stmp;
bool match(int l1, int r1, int l2, int r2)
{
for (int i=0; i<=r1-l1; i++) if (s[l1+i]!=s[l2+i]) return 0;
return 1;
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>stmp>>a>>b>>c;
for (int i=1; i<=n; i++) s[i]=stmp[i-1]-'a'+1;
for (int i=1; i<=n; i++) dp[i][i]=a;
for (int sz=1; sz<=n; sz++)
{
for (int l=1; l+sz<=n; l++)
{
int r=l+sz;
dp[l][r]=min(dp[l+1][r], dp[l][r-1])+a;
for (int md=l; md<=r; md++)
{
ll idx=md+1, cnt=1, blk=0;
while (idx<=r)
{
if (idx+md-l<=r&&match(l, md, idx, idx+md-l)) cnt++, idx=idx+md-l+1;
else idx++, blk++;
}
dp[l][r]=min(dp[l][r], dp[l][md]+blk*a+c*cnt+b);
}
}
}
cout<<dp[1][n];
}
# | 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... |