zoukankan      html  css  js  c++  java
  • 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
    ", 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 }
    View Code
  • 相关阅读:
    解决Original error: Could not proxy command to remote server. Original error: Error: socket hang up
    python各进制转换
    爬楼梯问题,yield学习总结
    微信开放平台API开发资料
    数据切分——Atlas读写分离Mysql集群的搭建
    svn SSL 错误:Key usage violation in certificate has been detected
    如何将一个空间里的内容全部复制到另一个空间,文件名不变
    SVN客户端安装 Linux
    Web端即时通讯技术盘点:短轮询、Comet、Websocket、SSE
    搭建SVN服务器
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3850513.html
Copyright © 2011-2022 走看看