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 ...
随机推荐
- 前端之JavaScript 补充
1. BOM window location.href = "https://www.sogo.com" location.reload() // 重新加载当前页 location ...
- HDU - 6433: H. Pow (简答题,输出大数)
There are n numbers 3^0, 3^1, . . . , 3^n-1. Each time you can choose a subset of them (may be empty ...
- Tomcat启动超时问题Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds
使用Eclipse启动Tomcat时出现启动超时的问题如下所示: Server Tomcat v7.0 Server at localhost was unable to start within 4 ...
- Mybatis与Hibernate的详细对比
前言 这篇博文我们重点分析一下Mybatis与hibernate的区别,当然在前面的博文中我们已经深入的研究了Mybatis和Hibernate的原理. Mybatis [持久化框架]Mybatis简 ...
- LeetCode Construct Binary Tree from String
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-string/description/ 题目: You need to ...
- gradle wrapper 简单使用
其实就是对于gradle 的一个包装,保证了项目版本的一致,同时减少配置 1. 生成wrapper // 使用gradle wrapper 命令 gradle wrapper 输出效果如下: [r ...
- ASP.NET Cache缓存的使用
ASP.NET Cache是提升系统性能的重要方法,它使用了“最近使用”原则(a least-recently-used algorithm).在数据库访问中经常会用到Cache保存数据库数据. 1. ...
- Oracle查询脚本优化
发现生产环境的Oracle数据库cpu使用率上升超过70%,其中一条查询语句达到每秒调用40多次.现在我们来观摩下该语句: select t.id,t.level,t.policy, t.type,t ...
- 关于yii2 REST api 的问题
首先,需要在basic/web/文件夹下添加一个.htaccess文件 这样进入项目就会自动访问index.php文件,url就不会错乱了 <IfModule mod_rewrite.c> ...
- 完整的qt安装教程
大家可能认为qt收费了 其实不是 大家直接点击 这个 Community 这里的下载 Download 然后呢 就跳转到这个界面 点击 Qt Offline Installer 就会跳到这个地方 滑下 ...