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. nyoj 304 节能 【区间dp】

    点击打开链接 节能 时间限制:1000 ms  |  内存限制:65535 KB 难度:5 描述 Dr.Kong设计的机器人卡多越来越聪明.最近市政公司交给卡多一项任务,每天早晨5:00开始,它负责关 ...

  2. (NYoj 304) 节能 --区间DP

    节能 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描述 Dr.Kong设计的机器人卡多越来越聪明.最近市政公司交给卡多一项任务,每天早晨5:00开始,它负责关掉ZK大道右侧上所 ...

  3. [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 ...

  4. nyoj 304(区间dp)

    解题思路:这道题很明显是用区间dp,可是与以往的区间dp不同,因为对于区间[i,j],机器人所处的位置要么在i,要么在j(因为机器人要移动到某一点才能关闭灯泡,所以对于某一段区间来说,机器人最后肯定在 ...

  5. HDU-2476 String painter 区间DP

    题意:给你一个长度相等的A串和B串,每次可以把一个连续的区间刷成一个字母,问从A串到B串的最少操作数. 解法:虽然这类题一看到就知道是区间DP,但是之前只做过类似从空串变成某个串的题目,所以没想到怎么 ...

  6. HDU 2476 String painter (区间DP)

    题意:给出两个串a和b,一次只能将一个区间刷一次,问最少几次能让a=b 思路:首先考虑最坏的情况,就是先将一个空白字符串刷成b需要的次数,直接区间DP[i][j]表示i到j的最小次数. 再考虑把a变成 ...

  7. LA 4394 String painter 区间DP -

    题目地址:http://vjudge.net/problem/UVALive-4394 很明显的区间DP 区间DP的套路就是 d[i][j]的在区间 (i,j) 刷的次数 转移也一般是 d[i][j] ...

  8. 合并石子 区间dp水题

    合并石子 链接: nyoj 737 描述: 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的两堆石子堆成一堆,每次合并花费的代价为这两堆石子的和,经过N- ...

  9. POJ 2955 Brackets (区间DP)

    题目链接:http://poj.org/problem?id=2955 Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

最新文章

  1. 剑指offer:面试题32 - III. 从上到下打印二叉树 III
  2. Apache Kafka-初体验Kafka(02)-Centos7下搭建单节点kafka_配置参数详解_基本命令实操
  3. Py之mglearn:python库之mglearn简介、安装、使用方法之详细攻略
  4. IntelliJ Idea工具使用
  5. Java获取文件大小,文件夹内文件个数的工具类
  6. 使用Jupyter notebook,按下ctrl+enter后,一直出现In[*]呢?
  7. 【Protocol Buffer】Protocol Buffer入门教程(六):枚举和包
  8. Windows10 部署 Sonarqube 代码质量管理平台
  9. 进程调度:优先数法与轮转法的实现
  10. EasyAndroid基础集成组件库之:EasyReflect 优雅的反射功能库
  11. C++中使用GSoap
  12. Fiddler4的安装与使用
  13. si24r1程序_SI24R1技术支持--程序 射频识别(radio frequency indentificationx) - 下载 - 搜珍网...
  14. 网易交互设计师微专业 目录
  15. 谈google搜索引擎的使用
  16. JMeter性能测试实战
  17. NLP 2.9 深度学习与神经网络
  18. python常量列表_秦路天善智能python学习笔记1-数据类型,常量,变量,列表,字典,元组...
  19. Itext与pdfBox坐标定位问题
  20. Modifiers should be declared in the correct order 修饰符应按正确的顺序声明

热门文章

  1. ActiveMQ消息传递的两种方式
  2. 友友企业地图(Enterprise MAP)
  3. Maven pom.xml配置详解(三)
  4. C++学习之路: 单例模板
  5. 一个简单的apache cgi-bin
  6. 【转贴】Decoda Tutorial LUA调式器
  7. 两个datatable之间的复制
  8. Recommended Journals for MPhil degree at Business Model Innovation Group
  9. 我希望的未来职业发展!!!!!!!!!!想了半年的最终的结果~好像又没有变化哈哈哈哈
  10. 分组背包基础--1712 ACboy needs your help