String Compression

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Appoint description: 
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的更多相关文章

  1. 区间DP UVA 1351 String Compression

    题目传送门 /* 题意:给一个字符串,连续相同的段落可以合并,gogogo->3(go),问最小表示的长度 区间DP:dp[i][j]表示[i,j]的区间最小表示长度,那么dp[i][j] = ...

  2. uva live 4394 String painter 间隔dp

    // uva live 4394 String painter // // 问题是,在培训指导dp运动主题,乍一看,我以为只是一点点复杂 // A A磕磕磕,两个半小时后,.发现超过例子.然而,鉴于他 ...

  3. 【leetcode】443. String Compression

    problem 443. String Compression Input ["a","a","b","b"," ...

  4. 443. String Compression

    原题: 443. String Compression 解题: 看到题目就想到用map计数,然后将计数的位数计算处理,这里的解法并不满足题目的额外O(1)的要求,并且只是返回了结果array的长度,并 ...

  5. CF825F String Compression 解题报告

    CF825F String Compression 题意 给定一个串s,其中重复出现的子串可以压缩成 "数字+重复的子串" 的形式,数字算长度. 只重复一次的串也要压. 求压缩后的 ...

  6. 213. String Compression【LintCode java】

    Description Implement a method to perform basic string compression using the counts of repeated char ...

  7. 213. String Compression【easy】

    Implement a method to perform basic string compression using the counts of repeated characters. For ...

  8. codeforces 825F F. String Compression dp+kmp找字符串的最小循环节

    /** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...

  9. Codeforces 825F - String Compression

    825F - String Compression 题意 给出一个字符串,你要把它尽量压缩成一个短的字符串,比如一个字符串ababab你可以转化成3ab,长度为 3,比如bbbacacb转化成3b2a ...

随机推荐

  1. OpenStack 界面开发中的排序问题

    Contents [hide] 1 需求 2 调研 3 排序的办法 4 解决代码 需求 获取主机列表的时候,希望能够对主机列表能分组显示,比如网络,一组网络段希望在一起显示 调研 openstack的 ...

  2. IMG图片和文字同行显示

    只要设定img标签的vertical-align CSS属性就好了,代码如下: <img src="images/untitled.png" style="widt ...

  3. 160929、各数据库连接配置与maven依赖安装

    最近做的项目都是maven的,据说maven是个东西.把依赖的jar文件的事情都委托出去辣!试着用了一下哈,效果还可以! 今天做了数据库配置这一块,特意把相关的东西总结出来,以备不时之需. MySQL ...

  4. declare和typeset DEMO

    declare=typeset,用法完成相同. declare不指定变量:显示所有变量的值. -r选项,把指定变量定义为只读变量: [xiluhua@vm-xiluhua][~]$ declare - ...

  5. linux man

    man能够为除命令之外的配置文件.系统调用.库调用等都能提供帮助手册,他们分别位于不同的章节中: 1.用户命令 2.系统调用 3.库调用 4.设备文件 5.配置文件 6.游戏 7.杂项 8.管理命令

  6. 安装centos7.1 32bit时,没有可用的网络设备的解决方法

    安装的系统镜像文件:CentOS-7-i386-LiveGNOME-1511.iso 虚拟机版本: 问题: 原因: 原先我在这里选择的时候,以为自己安装的不是64位的,所以没有选择centos 64, ...

  7. OC基础数据类型-NSData

    1.NSData,数据,当我们需要把一些信息写入到文件里或发送到网络上,我们需要把这些数据转换下,变成纯粹的0.1字符流 1 NSString * str = @"hello, world! ...

  8. jQuery_Ajax_Json 异步接收PHP端传来的json数据

    [json]{"user_id":"1172940","rmb_point":"0","weixin_id&q ...

  9. CodeIgniter配置之config

    配置说明 $config['language']:指定项目语言包.需要注意的时Codeigniter自带的类库错误提示语言包位于/system/language/english/目录下,当这里配置非e ...

  10. 每日一九度之 题目1031:xxx定律

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6870 解决:4302 题目描述:     对于一个数n,如果是偶数,就把n砍掉一半:如果是奇数,把n变成 3*n+ 1后砍掉一半,直到该数 ...