题意:给你一个字符串,有2种消除方式:1:消除一个单独的字母,代价为a。2:s[j]到s[k]是s[1]到s[j - 1]的子串,那么s[j]到s[k]可以消除,代价为b,问最小的代价。

思路:官方题解说的很明白了。

代码:

#include <bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 5010;
int dp[maxn];
int v[maxn][maxn];
int Next[maxn];
char s[maxn];
int n, a, b;
int main() {
// freopen("inupt.txt", "r", stdin);
scanf("%d%d%d", &n, &a, &b);
scanf("%s",s + 1);
memset(dp, 0x3f, sizeof(dp));
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j++) {
if(s[i] == s[j]) {
v[i][j] = v[i - 1][j - 1] + 1;
}
}
dp[0] = 0;
for (int i = 1; i <= n; i++) {
dp[i] = dp[i - 1] + a;
for (int j = 1; j < i; j++) {
int t = min(i - j, v[j][i]);
if(t) {
dp[i] = min(dp[i], dp[i - t] + b);
}
}
}
printf("%d\n", dp[n]);
}

  

Codeforces 1120C Compress String(DP)的更多相关文章

  1. codeforces#1120C. Compress String(dp+后缀自动机)

    题目链接: https://codeforces.com/contest/1120/problem/C 题意: 从前往后压缩一段字符串 有两种操作: 1.对于单个字符,压缩它花费$a$ 2.对于末尾一 ...

  2. NYOJ 1067 Compress String(区间dp)

    Compress String 时间限制:2000 ms  |  内存限制:65535 KB 难度:3 描写叙述 One day,a beautiful girl ask LYH to help he ...

  3. [CareerCup] 1.5 Compress String 压缩字符串

    1.5 Implement a method to perform basic string compression using the counts of repeated characters. ...

  4. [Codeforces 1201D]Treasure Hunting(DP)

    [Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...

  5. [Algo] 611. Compress String II

    Given a string, replace adjacent, repeated characters with the character followed by the number of r ...

  6. 【Codeforces 1120C】Compress String

    Codeforces 1120 C 题意:给一个串\(S\),将这个串分成\(t_1..t_m\),如果\(t_i\)在\(t_1..t_{i-1}\)中作为子串出现过,那么这个的代价是\(b\),否 ...

  7. Educational Codeforces Round 16 E. Generate a String dp

    题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 seco ...

  8. codeforces 825F F. String Compression dp+kmp找字符串的最小循环节

    /** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...

  9. codeforces 710E E. Generate a String(dp)

    题目链接: E. Generate a String time limit per test 2 seconds memory limit per test 512 megabytes input s ...

随机推荐

  1. Chrome设置允许ajax跨域

    最近在做一个前后端分离的项目,在Windows上用.Net WebApi时候的后端,在Mac上用ng2搭建的前端. 要实现前后端对接进行调试,就必须要解决ajax跨域的问题,实现方法如下: //在te ...

  2. 一个通用Makefile详解

    我们在Linux环境下开发程序,少不了要自己编写Makefile,一个稍微大一些的工程下面都会包含很多.c的源文 件. 如果我们用gcc去一个一个编译每一个源文件的话,效率会低很多,但是如果我们可以写 ...

  3. Proposition

    提供 \(k\) 个变量 \((k\leq 4)\) 可独立取值为 \(0,1\),两种运算分别等价于 \(\neg a\) 和 \(\neg a \lor b\) . 你需要恰好使用 \(n\) 个 ...

  4. bzoj 1725 Corn Fields

    Written with StackEdit. Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行\((1<=M<=12; 1<=N< ...

  5. 【转】C# Socket编程(2)识别网络主机

    [转自:https://www.cnblogs.com/IPrograming/archive/2012/10/11/CSharp_Socket_2.html] 一个客户端想要发起一次通信,先决条件就 ...

  6. 【WCF安全】WCF 自定义授权[用户名+密码+x509证书]

    1.x509证书制作(略) 2.直接贴代码 ----------------------------------------------------------------------服务端----- ...

  7. 一步一步建立 webpack 4 项目

    一步一步建立 webpack 4 项目 cnpm init -y cnpm i webpack --save-dev cnpm i webpack-cli --save-dev 修改 package. ...

  8. tomcat启动报错:Bean name 'XXX' is already used in this <beans> element

    如题,tomcat容器启动时加载spring的bean,结果报错如下: 六月 28, 2017 9:02:25 上午 org.apache.tomcat.util.digester.SetProper ...

  9. java输入月份,年份,显示对应月份的天数,

    总结:1,输入月份,年份,这需要用Scanner   2.我们已知道12个月份的天数,有30天,31天   3.判断闰年 用switch -case-break语句  4.注意不要忘了写break;判 ...

  10. 杂项-公司-百科:伯克希尔·哈撒韦-un

    ylbtech-杂项-公司-百科:伯克希尔·哈撒韦 伯克希尔·哈撒韦公司由沃伦·巴菲特(Warren Buffett)创建于1956年,是一家主营保险业务,在其他许多领域也有商业活动的公司.其中最重要 ...