zoukankan      html  css  js  c++  java
  • UVA 1351 十三 String Compression

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

    Description

    Download as PDF
     

    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
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 char s[205];
     7 int dp[205][205];
     8 
     9 int check(int l,int r,int k)
    10 {
    11     int i,j;
    12     if(l>r)
    13         return 0;
    14     for(i=l;i<=r;i++)
    15     {
    16         for(j=0;i+j*k<=r;j++)
    17         {
    18             if(s[i]!=s[i+j*k])
    19                 return 0;
    20         }
    21     }
    22     return 1;
    23 }
    24 
    25 int digit(int a)
    26 {
    27     int s=0;
    28     while(a)
    29     {
    30         s++;
    31         a=a/10;
    32     }
    33     return s;
    34 }
    35 
    36 int main()
    37 {
    38     int T;
    39     int i,j,k,p,len;
    40     scanf("%d",&T);
    41     while(T--)
    42     {
    43         scanf("%s",s);
    44         len=strlen(s);
    45         for(i=0;i<len;i++)
    46         {
    47             for(j=0;j<len;j++)
    48             {
    49                 dp[i][j]=205;
    50             }
    51         }
    52         for(i=0;i<len;i++)
    53             dp[i][i]=1;
    54         for(i=2;i<=len;i++)
    55         {
    56             for(j=0;j+i-1<len;j++)
    57             {
    58                 int b=j,e=j+i-1;
    59                 for(k=b;k<e;k++)
    60                 {
    61                     dp[b][e]=min(dp[b][e],dp[b][k]+dp[k+1][e]);
    62                 }
    63                 for(p=1;p<=i/2;p++)
    64                 {
    65                     if(i%p==0)
    66                     {
    67                         if(check(b,e,p))
    68                         {
    69                             dp[b][e]=min(dp[b][e],digit(i/p)+2+dp[b][b+p-1]);
    70                         }
    71                     }
    72                 }
    73             }
    74         }
    75         printf("%d
    ",dp[0][len-1]);
    76     }
    77     return 0;
    78 }
    View Code
  • 相关阅读:
    学算法还能指导找对象?是的,这就是大名鼎鼎的稳定婚姻算法
    机器学习 | 详解GBDT梯度提升树原理,看完再也不怕面试了
    Python | 面试的常客,经典的生产消费者模式
    LeetCode 89,因为题目晦涩而被点了1500+反对的搜索问题
    LeetCode 87,远看是字符串其实是搜索,你能做出来吗?
    Golang | Go语言多态的实现与interface使用
    pandas | DataFrame基础运算以及空值填充
    算法数据结构 | 只要30行代码,实现快速匹配字符串的KMP算法
    Mac上使用Docker安装SQLServer
    [翻译]ASP.NET Core在 .NET 5 Preview 7的更新
  • 原文地址:https://www.cnblogs.com/cyd308/p/4771599.html
Copyright © 2011-2022 走看看