题意:给你一个字符串,有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. WampServer常用环境配置

    WampServer即Windows Apache Mysql PHP集成安装环境,就是在window下的apache.php和mysql的服务器软件. 其特点就是 1.支持中文语言,一键安装,省时省 ...

  2. Golang Printf、Sprintf 、Fprintf 格式化

    /* %v 输出结构体 {10 30} %+v 输出结构体显示字段名 {one:10 tow:30} %#v 输出结构体源代码片段 main.Point{one:10, tow:30} %T 输出值的 ...

  3. Docker从入门到安装MySQL

    Docker 的简介 Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后 ...

  4. 手把手教你在Eclipse中使用CVS Branch功能

    Brach 的作用: 开发新版本的人员就基于 main trunk 工作,而 fix bug 的人员就基于 branch 工作. 一旦在 branch上将 Release_1_0的 bug修复了,我们 ...

  5. spring mvc集成freemarker使用

    freemarker作为视图技术出现的比velocity早,想当年struts风靡一时,freemarker作为视图层也风光了一把.但现在velocity作为后起之秀的轻量级模板引擎,更容易得到青睐. ...

  6. 第15篇 PSR-04 规范

    1. Overview This PSR describes a specification for autoloading classes from file paths. It is fully ...

  7. springboot mybatisPlus配置

    1.pom依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  8. ADO连接ACCESS数据库

    首先在StdAfx.h中加入 建立连接:(在xxApp文件中) 1  声明变量 2 建立连接 (1) AfxOleInit 初始化 OLE 为应用程序的支持. BOOL AFXAPI AfxOleIn ...

  9. shell 正则表达式与文件名匹配

    1) . : 匹配任意单ASCII 字符,可以为字母,或为数字. 2) 举例: ..XC..匹配deXC1t.23XCdf 等,.w..w..w.匹配rwxrw-rw- 行首以^匹配字符串或字符序列 ...

  10. android签名生成和发布

    首先,我们需要一个keystore,当然已经有了的话就不用这一步了:cmd下:进入到jdk的bin目录,这样的话,android.keystore文件就会生成在这个目录下,签名的时候我们需要这个文件C ...