Compress String

时间限制:2000 ms  |  内存限制:65535 KB
难度:3
描写叙述
One day,a beautiful girl ask LYH to help her complete a complicated task—using a new compression method similar to Run Length Encoding(RLE) compress a string.Though the task is difficult, LYH is glad to help her.
The compress rules of the new method is as follows: if a substring S is repeated k times, replace k copies of S by k(S). For example, letsgogogo is compressed into lets3(go). The length of letsgogogo is
10, and the length of lets3(go) is 9. In general, the length of k(S) is (number of digits in k ) + (length of S) + 2. For example, the length of 123(abc) is 8. It is also possible that substring S
is a compressed string. For example nowletsgogogoletsgogogoandrunrunrun could be compressed as now2(lets3(go))and3(run).
In order to make the girl happy, LYH solved the task in a short time. Can you solve it?
输入
Thera are multiple test cases.

Each test case contains a string, the length of the string is no more than 200, all the character is lower case alphabet.
输出
For each test case, print the length of the shortest compressed string.
例子输入
ababcd
letsgogogo
nowletsgogogoletsgogogoandrunrunrun
例子输出
6
9
24

题意:给出一个长度不超过200的字符串。把这个字符串依照一定规则压缩,即能够把几个连续的同样子串压缩成一个串,比如能够把letsgogogo压缩为lets3(go),压缩后的子串假设还能够继续压缩。则能够继续压缩,如能够将nowletsgogogoletsgogogoandrunrunrun压缩为now2(lets3(go))and3(run)。问经过压缩后这个字符串的最短长度是多少。

分析: 区间DP,dp[i][j]表示从i到j的字符串表示的最短长度。

dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j])。

然后去推断当前子串能不能压缩。即是否由反复字符串组成,推断时仅仅需暴力枚举反复长度,去推断就可以。

          假设当前子串能够压缩,则dp[i][j] = min(dp[i][j], dp[i][i + len - 1] + 2 + digcount((j - i + 1) / len));。

注意假设是数字,要用数字的位数表示添加的个数。而不是单纯的添加1.

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 210;
#define INF 0x3fffffff
char str[N];
int n, dp[N][N]; int digit_cnt(int x)
{
int a = 0;
while(x) {
a++;
x /= 10;
}
return a;
} bool check(int l, int r, int len)
{
if((r - l + 1) % len) return false;
for(int i = l; i < l + len; i++) {
for(int j = i + len; j <= r; j += len)
if(str[i] != str[j]) return false;
}
return true;
} int get_ans()
{
int i, j, k;
n = strlen(str+1);
for(i = 1; i <= n; i++) dp[i][i] = 1;
for(i = n - 1; i >= 1; i--) {
for(j = i + 1; j <= n; j++) {
dp[i][j] = INF;
for(k = i; k < j; k++)
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j]);
for(int len = 1; len <= j-i+1; len++) {
if(check(i, j, len)) {
dp[i][j] = min(dp[i][j], dp[i][i+len-1] + 2 + digit_cnt((j - i + 1) / len));
}
}
}
}
return dp[1][n];
} int main()
{
while(~scanf("%s", str+1)) {
printf("%d\n", get_ans());
}
return 0;
}

NYOJ 1067 Compress String(区间dp)的更多相关文章

  1. Codeforces Round #354 (Div. 2)-C. Vasya and String,区间dp问题,好几次cf都有这种题,看来的好好学学;

    C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. nyoj 460 项链 (区间dp)

    项链 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 在Mars星球上,每个Mars人都随身佩带着一串能量项链.在项链上有N颗能量珠.能量珠是一颗有头标记与尾标记的珠子, ...

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

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

  4. eduCF#61 F. Clear the String /// 区间DP 消除连续一段相同字符 全部消完的最少次数

    题目大意: 给定字符串 每次消除可消除连续的一段相同的字符的子串 求消除整个字符串的最少消除次数 #include <bits/stdc++.h> using namespace std; ...

  5. hdu2476 String painter(区间dp)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2476 Problem Description There are two strings ...

  6. nyoj 737 石子合并(一)。区间dp

    http://acm.nyist.net/JudgeOnline/problem.php?pid=737 数据很小,适合区间dp的入门 对于第[i, j]堆,无论你怎么合并,无论你先选哪两堆结合,当你 ...

  7. NYOJ 题目15 括号匹配(二)(区间DP)

    点我看题目 题意 : 中文题不详述. 思路 : 本来以为只是个小模拟,没想到是个区间DP,还是对DP不了解. DP[i][j]代表着从字符串 i 位置到 j 位置需要的最小括号匹配. 所以初始化的DP ...

  8. [CF1107E]Vasya and Binary String【区间DP】

    题目描述 Vasya has a string s of length n consisting only of digits 0 and 1. Also he has an array a of l ...

  9. HDU2476 String painter——区间DP

    题意:要把a串变成b串,每操作一次,可以使a串的[l,r]区间变为相同的一个字符.问把a变成b最少操作几次. 这题写法明显是区间dp ,关键是处理的方法. dp[l][r]表示b串的l~r区段至少需要 ...

随机推荐

  1. if-else优化

    过多if-else分支的优化   超过3个就应该去优化,说if-else过多的分支可以使用switch或者责任链模式等等方式来优化.确实,这是一个小问题,不过我们还是可以整理一下这个小问题的重构方式. ...

  2. 解决img标签上下出现间隙的方法

    图片与父元素下边缘有 2px 的间隙,并不是因为空格.多个 inline-block 元素之间的间隙才是因为空格. 任何不是块级元素的可见元素都是内联元素,其表现的特性是“行布局”形式.----< ...

  3. 输入url后发生了什么

    (1)浏览器解析 (2)查询缓存 (3)DNS查询 顺序如下,若其中一步成功直接进去建立连接部分: -- 浏览器自身DNS -- 操作系统DNS -- 本地hosts文件 -- 像域名服务器发送请求 ...

  4. 【LeetCode】Count and Say(报数)

    这道题是LeetCode里的第38道题. 题目要求: 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 1 2. 11 3. 21 4. 1211 5. 111 ...

  5. Educational Codeforces Round 31

    A. Book Reading time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  6. [BZOJ2342] [Shoi2011]双倍回文(manacher)

    传送门 manacher...... 先跑一边manacher是必须的 然后枚举双倍回文串的对称轴x 把这个双倍回文串分成4段,w wR w wR 发现,只有当 y <= x + p[x] / ...

  7. VS链接错误: LNIK1123

    问题:编译一个VS工程程序,出现连接错误:"LNK1123: 转换到 COFF 期间失败: 文件无效或损坏" 原因分析:连接器LNK是通过调用cvtres.exe完成文件向coff ...

  8. 【CCF】路径压缩 区间dp

    [题意] 改编哈夫曼树,限制从左到右字母的编码按字典序递增 [思路] 因为是二进制编码,所以是二叉树: 因为是前缀码,所以每个字母都是叶子结点,不可能是内结点: 因为要按字典序递增,所以只能是相邻的结 ...

  9. charts jupyter notebook 画简单的柱状图

    数据库是mongdb 数据是58同城上发的转手记录 一 为了保证数据安全,对需要进行处理的数据进行拷贝. > db.createCollection('test') { } > show ...

  10. 单点登录跳转失败(原因是 主票据申请子票据失败) asp.net 同站点下不同应用间不同版本Framework问题

    单点登录跳转失败(原因是 主票据申请子票据失败) asp.net 同站点下不同应用间不同版本Framework问题 今天遇到一个问题,在主站点现在配置的应用和主站点登录会话状态不能共享,进入子站点应用 ...