Codeforces 1120C Compress String(DP)
题意:给你一个字符串,有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)的更多相关文章
- codeforces#1120C. Compress String(dp+后缀自动机)
题目链接: https://codeforces.com/contest/1120/problem/C 题意: 从前往后压缩一段字符串 有两种操作: 1.对于单个字符,压缩它花费$a$ 2.对于末尾一 ...
- NYOJ 1067 Compress String(区间dp)
Compress String 时间限制:2000 ms | 内存限制:65535 KB 难度:3 描写叙述 One day,a beautiful girl ask LYH to help he ...
- [CareerCup] 1.5 Compress String 压缩字符串
1.5 Implement a method to perform basic string compression using the counts of repeated characters. ...
- [Codeforces 1201D]Treasure Hunting(DP)
[Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...
- [Algo] 611. Compress String II
Given a string, replace adjacent, repeated characters with the character followed by the number of r ...
- 【Codeforces 1120C】Compress String
Codeforces 1120 C 题意:给一个串\(S\),将这个串分成\(t_1..t_m\),如果\(t_i\)在\(t_1..t_{i-1}\)中作为子串出现过,那么这个的代价是\(b\),否 ...
- 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 ...
- codeforces 825F F. String Compression dp+kmp找字符串的最小循环节
/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...
- 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 ...
随机推荐
- RPi 2B QEMU 模拟树莓派
/******************************************************************************** * RPi 2B QEMU 模拟树莓 ...
- 如何用 php 读取一个很大的 excel 文件。
这个程序是用php 读取一个很大的excel文件, 先将 excel 文件保存成csv 文件, 然后利用 迭代器 逐行读取 excel 单元格的值, 拿到值以后 做相应处理,并打印结果. <?p ...
- SqlServer 数据库负载均衡【转】
负载均衡集群是由一组相互独立的计算机系统构成,通过常规网络或专用网络进行连接,由路由器衔接在一起,各节点相互协作.共同负载.均衡压力,对客户端来说,整个群集可以视为一台具有超高性能的独立服务器. 1. ...
- WCF 配置文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.S ...
- fn project 打包Function
Option 1 (recommended): Use the fn cli tool We recommend using the fn cli tool which will handle a ...
- EL and JSTL(Jsp Standard Tag Libary)(转)
一.什么是 EL 语言. 表达式语言(EL)是 JSP 2.0 引入的一种计算和输出 Java 对象的简单语音. 二.EL 语言的作用. 为了使JSP写起来更加简单.表达式语言的灵感来自于 ECMAS ...
- log框架集成
使用slf4j,slf4j相当于一个接口,我们面对接口编程,方便地集成其他的日志框架,我们按照slf4j的标准,日志就会相应地打入日志系统中(log4j 使用slf4j要有两个包1,他本身的api,2 ...
- PDF通过剪裁来实现打印字体变大
之前打印论文,很多都是一页两版,这个时候字体会非常小:打印缩放放大后,字又容易出了打印边界. 这种情况可以采用Adobe IX Pro(只要是pro应该都可以)进行边缘裁剪来实现字体放大.只需要剪裁( ...
- bzoj 4104 [Thu Summer Camp 2015]解密运算——思路
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4104 想了很久,想出一个 nlogn (也许是 n2logn )的,可惜空间是 n2 . 已 ...
- linux之使用rpmbuild打rpm包
linux之使用rpmbuild打rpm包 前言: 已从事linux运维工作数年,感觉自己还是个小菜鸟,没有大神那么的钻研的精神.只是单纯热爱,喜欢对着黑色的屏幕敲击命令,喜欢这种感觉.为什么要做RP ...