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 string abcccddddddefgggggggggghijk is encoded into a stringab3c6def10ghijk 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 设dp[l][r]为区间l到r的最小值
则dp[l][r] = min(dp[l][k] + dp[k + 1][r], v);
v = dignum(len / k) + 2 + dp[l][l + k - 1]; (区间l r的字符串可以由连续d / k 个以l为起点长度为k的字符串组成)
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue> using namespace std; #define read() freopen("sw.in", "r", stdin) const int MAX = ;
int n;
char str[MAX];
int dp[MAX][MAX]; int cal(int k) {
int ret = ;
if (!k) ++ret;
while (k) {
ret += ;
k /= ;
}
return ret;
} bool check(int l, int r, int k) {
for (int i = l + k; i <= r; i += k) {
for (int j = i; j < i + k; ++j)
if (str[j] != str[j - k]) return false;
}
return true;
} void solve() { for (int len = ; len <= n; ++len) {
for (int l = ; l + len - < n; ++l) {
int r = l + len - ;
dp[l][r] = len;
for (int k = l; k <= r; ++k) {
if (k + > r) continue;
dp[l][r] = min(dp[l][r], dp[l][k] + dp[k + ][r]);
} for (int k = ; k <= len / ; ++k) {
if (len % k != ) continue;
if (check(l, r, k)) {
dp[l][r] = min(dp[l][r], cal(len / k) + + dp[l][l + k - ]);
}
} }
} printf("%d\n", dp[][n - ]);
} int main()
{ int t;
// read();
scanf("%d", &t);
while (t--) {
scanf("%s", str);
n = strlen(str);
solve();
}
//cout << "Hello world!" << endl;
return ;
}

LA 3363的更多相关文章

  1. leggere la nostra recensione del primo e del secondo

    La terra di mezzo in trail running sembra essere distorto leggermente massima di recente, e gli aggi ...

  2. Le lié à la légèreté semblait être et donc plus simple

    Il est toutefois vraiment à partir www.runmasterfr.com/free-40-flyknit-2015-hommes-c-1_58_59.html de ...

  3. Mac Pro 使用 ll、la、l等ls的别名命令

    在 Linux 下习惯使用 ll.la.l 等ls别名的童鞋到 mac os 可就郁闷了~~ 其实只要在用户目录下建立一个脚本“.bash_profile”, vim .bash_profile 并输 ...

  4. BZOJ 3363: [Usaco2004 Feb]Cow Marathon 奶牛马拉松

    Description 给你一个图,两个点至多有一条路径,求最长的一条路径. \(n \leqslant 4\times 10^4\) Sol DFS?DP? 这就是一棵树,方向什么的都没用... 然 ...

  5. Linux中的动态库和静态库(.a/.la/.so/.o)

    Linux中的动态库和静态库(.a/.la/.so/.o) Linux中的动态库和静态库(.a/.la/.so/.o) C/C++程序编译的过程 .o文件(目标文件) 创建atoi.o 使用atoi. ...

  6. Mac OS使用ll、la、l等ls的别名命令

    在linux下习惯使用ll.la.l等ls别名的童鞋到mac os可就郁闷了-- 其实只要在用户目录下建立一个脚本“.bash_profile”,并输入以下内容即可: alias ll='ls -al ...

  7. .Uva&LA部分题目代码

    1.LA 5694 Adding New Machine 关键词:数据结构,线段树,扫描线(FIFO) #include <algorithm> #include <cstdio&g ...

  8. 获取在线人数 CNZZ 和 51.la

    string Cookies = string.Empty; /// <summary> /// 获取在线人数 (51.la统计器) /// </summary> /// &l ...

  9. BNU OJ 33691 / LA 4817 Calculator JAVA大数

    留着当个模板用,在BNU上AC,在LA上RE……可能是java的提交方式不同??? 数和运算符各开一个栈. 表达式从左到右扫一遍,将数存成大数,遇到数压在 数的栈,运算符压在 运算符的栈,每当遇到右括 ...

随机推荐

  1. EC2:将80端口直接转向8080端口

    当安装Tomcat后,需要将服务器的80端口直接指向8080端口,做法如下:1.    确认当前状态.在终端键入:netstat –ntl命令.输入结果应该和下面图片类似.可以看到8080端口处于监听 ...

  2. 【LeetCode】Longest Substring Without Repeating Characters 解题报告

    [题意] Given a string, find the length of the longest substring without repeating characters. For exam ...

  3. TTS-零基础入门之停止列表中单条语音播报

    做了一个语音 循环播报列表信息.当我新删除了一天列表之后,发现它仅仅有在下一轮播报中才会取消.这明显是不合理的. 一開始的代码是这样写的. <span style="font-fami ...

  4. SerializeUtil 序列化,反序列化工具类

    package cloud.app.prod.home.utils; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutp ...

  5. 【Ubuntu】基本操作 (条目=11)

    定义 NAME 为要操作的对象名 定义 DIR 为文件所在的绝对路径 所有操作默认在普通用户下进行 所有软件包默认是指Debian包(deb包) 1.查看进程 top 2.强制结束进程 PID由top ...

  6. 虚函数的特点就是执行的时候会下降到子类去执行同名覆盖函数 good

    var t: TBitBtn;begin t:=TBitBtn.Create(nil); t.Name:='BitBtn100'; t.parent :=Self; // 这里下断点end; 一路跟踪 ...

  7. Android按键添加和处理的方案【转】

    本文转载自:http://www.cnblogs.com/skywang12345/p/3142851.html Android按键添加和处理的方案  版本号 说明 作者 日期  1.0  Andro ...

  8. What is the difference between task and thread?

    http://stackoverflow.com/questions/4130194/what-is-the-difference-between-task-and-thread 回答一: A tas ...

  9. [python 基础]python装饰器(二)带参数的装饰器以及inspect.getcallargs分析

    带参数的装饰器理解无非记住两点: 1.本质不过在基本的装饰器外面再封装一层带参数的函数 2.在使用装饰器语法糖的时候与普通装饰器不同,必须要加()调用,且()内的内容可以省略(当省略时,admin默认 ...

  10. go之数组

    一.数组概念 go语言提供了数组类型的数据结构 数组是具有 [唯一类型] 的一组 [固定长度] 的数据项序列,这种类型可以是任意类型 二.数组声明 var variable_name [SIZE]va ...