LA 3363

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的字符串组成)

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <queue>
 7
 8 using namespace std;
 9
10 #define read() freopen("sw.in", "r", stdin)
11
12 const int MAX = 205;
13 int n;
14 char str[MAX];
15 int dp[MAX][MAX];
16
17 int cal(int k) {
18         int ret = 0;
19         if (!k) ++ret;
20         while (k) {
21                 ret += 1;
22                 k /= 10;
23         }
24         return ret;
25 }
26
27 bool check(int l, int r, int k) {
28         for (int i = l + k; i <= r; i += k) {
29                 for (int j = i; j < i + k; ++j)
30                 if (str[j] != str[j - k]) return false;
31         }
32         return true;
33 }
34
35 void solve() {
36
37         for (int len = 1; len <= n; ++len) {
38                 for (int l = 0; l + len - 1 < n; ++l) {
39                         int r = l + len - 1;
40                         dp[l][r] = len;
41                         for (int k = l; k <= r; ++k) {
42                                 if (k + 1 > r) continue;
43                                 dp[l][r] = min(dp[l][r], dp[l][k] + dp[k + 1][r]);
44                         }
45
46                         for (int k = 1; k <= len / 2; ++k) {
47                                 if (len % k != 0) continue;
48                                 if (check(l, r, k)) {
49                                         dp[l][r] = min(dp[l][r], cal(len / k) + 2 + dp[l][l + k - 1]);
50                                 }
51                         }
52
53                 }
54         }
55
56         printf("%d\n", dp[0][n - 1]);
57 }
58
59 int main()
60 {
61
62     int t;
63   //  read();
64     scanf("%d", &t);
65     while (t--) {
66             scanf("%s", str);
67             n = strlen(str);
68             solve();
69     }
70     //cout << "Hello world!" << endl;
71     return 0;
72 }

LA 3363

时间: 2024-11-05 11:58:05

LA 3363的相关文章

Hdu 3363 Ice-sugar Gourd(思路)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3363 思路:有解:两种情况.第一种,中间一分刚好,即切一刀.第二种,将第一个与最后一个连接起来,组成一个圆,过圆心将圆分成两部分,总有一种方式符合题意.枚举起点i,判断sum[i+n/2]-sum[i]是否等于H/2即可. #include<cstdio> #include<vector> #include<cstring> #include<iostream>

hdu 5745 la vie en rose

这道题的官方题解是dp,但是可以暴力出来.改天再研究怎么dp. 暴力的时候,如果计算sum的时候,调用strlen函数会超时,可见这个函数并不是十分的好.以后能不用尽量不用. La Vie en rose Time Limit: 14000/7000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 861    Accepted Submission(s): 461 Problem

让MAC OS也能使用LL LA L等LS的别名

linux下默认ll是ls -l的别名.OS X下默认不支持.习惯了linux下使用ll,我们同样也可以将习惯搬到os x下的shell中. 再当前用户家目录下新建.bash_profile文件.根据你的习惯,添加下面格式内容即可. 1 2 3 alias ll='ls -l' alias la='ls -a' alias l='ls -la' 然后执行:source .bash_profile你还可以添加你喜欢的其他别名.

LA 3942 Remember the Word (Trie)

Remember the Word 题目:链接 题意:给出一个有S个不同单词组成的字典和一个长字符串.把这个字符串分解成若干个单词的连接(单词可以重复使用),有多少种方法? 思路:令d[i]表示从字符i开始的字符串(后缀s[i..L])的分解数,这d[i] = sum{d(i+len(x)) | 单词x是其前缀}.然后将所有单词建成一个Trie树,就可以将搜索单词的复杂度降低. 代码: #include<map> #include<set> #include<queue>

LA 2678 Subsequence

有一个正整数序列,求最短的子序列使得其和大于等于S,并输出最短的长度. 用数组b[i]存放序列的前i项和,所以b[i]是递增的. 遍历终点j,然后在区间[0, j)里二分查找满足b[j]-b[i]≥S的最大的i,时间复杂度为O(nlongn). 这里二分查找用到库函数lower_bound() 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #inclu

LA 4127 - The Sky is the Limit (离散化 扫描线 几何模板)

题目链接 非原创 原创地址:http://blog.csdn.net/jingqi814/article/details/26117241 题意:输入n座山的信息(山的横坐标,高度,山底宽度),计算他们的轮廓线, 即露出来的表面边长,有些山是重叠的不计.空白地带不计,每座山都是等腰三角形. 分析:大白书P414页. 求小山的总长度,用一些虚线将其离散化,分成一段一段的,特征点:山脚,山顶,交点.这样就能保 证相邻两个扫描点之间再无交点.然后一最上面的点就是分割点,维护上一个点lastp即可. 1

LA 2031

Mr. White, a fat man, now is crazy about a game named ``Dance, Dance, Revolution". But his dance skill is so poor that he could not dance a dance, even if he dances arduously every time. Does ``DDR" just mean him a perfect method to squander his

Mac下ll、l、la、等简写命令不能使用

Mac默认用的也是Unix系统,Unix系统本身是没有这些简写命令的,可以通过给命令设置别名来使得可以使用这些简写命令 查看本机所有已经设置的命令别名:alias 设置命令别名:alias ll='ls -alF' 执行命令只在当前shell有效,要长期有效可以设置在用户的.bash_profile里面,这样每次登陆就都可以用这些简写命令了. 步骤: 1.vim ~/.bash_profile 2.将以下内容写入文件并保存 alias ll='ls -alF' alias la='ls -A'

Linux中.a,.la,.o,.so文件的意义和编程实现

Linux中.a,.la,.o,.so文件的意义和编程实现    Linux下文件的类型是不依赖于其后缀名的,但一般来讲:        .o,是目标文件,相当于windows中的.obj文件        .so 为共享库,是shared object,用于动态连接的,和dll差不多        .a为静态库,是好多个.o合在一起,用于静态连接        .la为libtool自动生成的一些共享库,vi编辑查看,主要记录了一些配置信息.可以用如下命令查看*.la文件的格式   $file