UVA 1351 十三 String Compression
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
System Crawler (2015-08-21)
Description
Run Length Encoding(RLE) is a simple form of compression. RLE consists of the process for searching for a repeated runs of a single character in a string to be compressed, and replacing them by a single instance of the character and a run count. For example, a stringabcccddddddefgggggggggghijk is encoded into a string ab3c6def10ghijk by RLE.
A new compression method similar to RLE is devised and the rule of the method is as follows: if a substring S<tex2html_verbatim_mark> is repeated k<tex2html_verbatim_mark> times, replace k<tex2html_verbatim_mark> copies of S<tex2html_verbatim_mark> by k(S)<tex2html_verbatim_mark> . 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)<tex2html_verbatim_mark> is (number of digits in k<tex2html_verbatim_mark> ) + (length of S<tex2html_verbatim_mark> ) + 2 (for `(' and `)'). For example, the length of 123(abc) is 8. It is also possible to nest compression, so the substring S<tex2html_verbatim_mark> may itself be a compressed string. For example,nowletsgogogoletsgogogo could be compressed as a now2(lets3(go)), and nowletsgogogoletsgogogoandrunrunrun could be compressed as now2(lets3(go))and3(run).
Write a program that, for a given string, gives a shortest compressed string using the compression rules as described above.
Input
Your program is to read from standard input. The input consists of T<tex2html_verbatim_mark> test cases. The number of test cases T<tex2html_verbatim_mark> is given in the first line of the input. Each test case consists of a single line containing one string of no more than 200 characters drawn from a lower case alphabet. The length of shortest input string is 1.
Output
Your program is to write to standard output. Print exactly one line for each test case. For each test case, print the length of the shortest compressed string.
The following shows sample input and output for four test cases.
Sample Input
4
ababcd
letsgogogo
nowletsgogogoletsgogogo
nowletsgogogoletsgogogoandrunrunrun
Sample Output
6
9
15
24
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; char s[];
int dp[][]; int check(int l,int r,int k)
{
int i,j;
if(l>r)
return ;
for(i=l;i<=r;i++)
{
for(j=;i+j*k<=r;j++)
{
if(s[i]!=s[i+j*k])
return ;
}
}
return ;
} int digit(int a)
{
int s=;
while(a)
{
s++;
a=a/;
}
return s;
} int main()
{
int T;
int i,j,k,p,len;
scanf("%d",&T);
while(T--)
{
scanf("%s",s);
len=strlen(s);
for(i=;i<len;i++)
{
for(j=;j<len;j++)
{
dp[i][j]=;
}
}
for(i=;i<len;i++)
dp[i][i]=;
for(i=;i<=len;i++)
{
for(j=;j+i-<len;j++)
{
int b=j,e=j+i-;
for(k=b;k<e;k++)
{
dp[b][e]=min(dp[b][e],dp[b][k]+dp[k+][e]);
}
for(p=;p<=i/;p++)
{
if(i%p==)
{
if(check(b,e,p))
{
dp[b][e]=min(dp[b][e],digit(i/p)++dp[b][b+p-]);
}
}
}
}
}
printf("%d\n",dp[][len-]);
}
return ;
}
UVA 1351 十三 String Compression的更多相关文章
- 区间DP UVA 1351 String Compression
		
题目传送门 /* 题意:给一个字符串,连续相同的段落可以合并,gogogo->3(go),问最小表示的长度 区间DP:dp[i][j]表示[i,j]的区间最小表示长度,那么dp[i][j] = ...
 - uva live 4394 String painter 间隔dp
		
// uva live 4394 String painter // // 问题是,在培训指导dp运动主题,乍一看,我以为只是一点点复杂 // A A磕磕磕,两个半小时后,.发现超过例子.然而,鉴于他 ...
 - 【leetcode】443. String Compression
		
problem 443. String Compression Input ["a","a","b","b"," ...
 - 443. String Compression
		
原题: 443. String Compression 解题: 看到题目就想到用map计数,然后将计数的位数计算处理,这里的解法并不满足题目的额外O(1)的要求,并且只是返回了结果array的长度,并 ...
 - CF825F String Compression 解题报告
		
CF825F String Compression 题意 给定一个串s,其中重复出现的子串可以压缩成 "数字+重复的子串" 的形式,数字算长度. 只重复一次的串也要压. 求压缩后的 ...
 - 213. String Compression【LintCode   java】
		
Description Implement a method to perform basic string compression using the counts of repeated char ...
 - 213. String Compression【easy】
		
Implement a method to perform basic string compression using the counts of repeated characters. For ...
 - codeforces 825F F. String Compression dp+kmp找字符串的最小循环节
		
/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...
 - Codeforces 825F - String Compression
		
825F - String Compression 题意 给出一个字符串,你要把它尽量压缩成一个短的字符串,比如一个字符串ababab你可以转化成3ab,长度为 3,比如bbbacacb转化成3b2a ...
 
随机推荐
- 《zw版·Halcon-delphi系列原创教程》halconxlib控件列表
			
<zw版·Halcon-delphi系列原创教程>halconxlib控件列表 Halcon v11.01版,com控件,安装后,共有75个控件, 不过最重要的控件,只有两个,T ...
 - zw版【转发·台湾nvp系列Delphi例程】HALCON DirectFile
			
zw版[转发·台湾nvp系列Delphi例程]HALCON DirectFile unit Unit1;interfaceuses Windows, Messages, SysUtils, Varia ...
 - zw版【转发·台湾nvp系列例程】HALCON MirrorRegion (Delphi)
			
zw版[转发·台湾nvp系列例程]HALCON MirrorRegion (Delphi) procedure TForm1.Button1Click(Sender: TObject);var img ...
 - mybatis 相关总结
			
1. Mybatis的<where><foreach><set>等标签详解:http://blog.csdn.net/zenson_g/article/detail ...
 - TI CC254x BLE教程 3
			
通用属性配置(Generic Attribute Profiles) 1. 数据在services里面以特征字(characteristics)的形式展示出来 如你所见, 特征字包含属性(Proper ...
 - powershell: 生成随机字符串
			
ASCII范围内的 获取6个随机字符(字母和数字) 48到57是数字0-9,powershell的范围操作符是..,和Perl 5的一样, 所以 48..57就是指(48 49 50 51 52 53 ...
 - Linux安装JDK、MyEclipse、MySql
			
一.将下载好的 jdk-7u79-linux-x64.tar.gz 复制到要安装的目录 /usr/lib/jdk (注:jdk文件夹自己创建 sudo mkdir jdk)二.进入到目录 /usr/l ...
 - settings.xml
			
<settings> <!--本地仓库.该值表示构建系统本地仓库的路径.其默认值为~/.m2/repository,windows:C:/Users/Administrator/.m ...
 - Intellij IDEA
			
http://1358440610-qq-com.iteye.com/blog/2102195
 - java 判断某一天是当年的哪一天
			
题目:输入年份,月份,日,判断这一天是这一年的第几天?(闰年的2月份为29天,平年为28天) public class Runnian { /** * 能被4整除且不能被100整除或者能被400整除的 ...