UVALive 3363 String Compression (区间DP,4级)

本文介绍了一种基于重复子串的字符串压缩方法,通过寻找并替换重复出现的子串来减少存储空间。讨论了压缩规则,并提供了一个实现该压缩算法的程序示例。
M - String Compression
Crawling in process... Crawling failed 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 string abcccddddddefgggggggggghijk 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 is repeated k times, replace k copies of S by k(S) . 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) is (number of digits in k ) + (length of S ) + 2 (for `(' and `)'). For example, the length of 123(abc) is 8. It is also possible to nest compression, so the substring S 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 test cases. The number of test cases T 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,划分区间而后合并,dp[i][j] 区间i-j的最优值。
     dp[i][j]=min(dp[i][k]+dp[k+1][j],i-j区间合并)
#include<iostream>
#include<cstdio>
#include<cstring>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const int mm=201;
int dp[mm][mm];
int bit[mm];
char s[mm];
bool ok(int l,int r,int dis)
{
  FOR(i,l,l+dis-1)
  for(int j=i+dis;j<=r;j+=dis)
    if(s[i]!=s[j])return 0;
  return 1;
}
void DP(int l,int r)
{
  if(l==r){dp[l][r]=1;return;}
  int&ret=dp[l][r];
  ret=mm;
  FOR(i,l,r-1)ret=min(ret,dp[l][i]+dp[i+1][r]);
  int len=r-l+1;
  //if(len&1)return;3的倍数也行 O(∩_∩)O~
  FOR(dis,1,len)
  { 
    if(len%dis==0&&ok(l,r,dis))//整数倍
      ret=min(ret,dp[l][l+dis-1]+2+bit[len/dis]);
  }
}
void BIT()
{
  FOR(i,0,9)bit[i]=1;
  FOR(i,10,99)bit[i]=2;
  FOR(i,100,200)bit[i]=3;
}
int main()
{
  int cas;BIT();
  while(~scanf("%d",&cas))
  {
    while(cas--)
    {
      scanf("%s",s);
      int len=strlen(s);
      clr(dp,-1);
      FOR(dis,1,len)
      for(int i=0;i+dis<=len;++i)
      DP(i,i+dis-1);
      printf("%d\n",dp[0][len-1]);
    }
  }
}


由于提供的引用内容未涉及K4os.Compression.LZ4.Streams的使用方法,下面基于一般知识给出其使用示例。 K4os.Compression.LZ4.Streams 是用于在 .NET 环境中使用 LZ4 压缩算法进行流压缩和解压缩的库。以下是基本的使用示例: ### 压缩流示例 ```csharp using System.IO; using K4os.Compression.LZ4.Streams; // 源文件路径 string sourceFilePath = "source.txt"; // 压缩后文件路径 string compressedFilePath = "compressed.lz4"; using (FileStream sourceStream = File.OpenRead(sourceFilePath)) using (FileStream compressedStream = File.Create(compressedFilePath)) using (LZ4EncoderStream encoderStream = LZ4Stream.Encode(compressedStream)) { sourceStream.CopyTo(encoderStream); } ``` ### 解压缩流示例 ```csharp using System.IO; using K4os.Compression.LZ4.Streams; // 压缩文件路径 string compressedFilePath = "compressed.lz4"; // 解压缩后文件路径 string decompressedFilePath = "decompressed.txt"; using (FileStream compressedStream = File.OpenRead(compressedFilePath)) using (FileStream decompressedStream = File.Create(decompressedFilePath)) using (LZ4DecoderStream decoderStream = LZ4Stream.Decode(compressedStream)) { decoderStream.CopyTo(decompressedStream); } ``` ### 代码解释 - **压缩部分**: - 打开源文件流和压缩后文件流。 - 使用 `LZ4Stream.Encode` 方法创建一个编码器流,将其包装在压缩后文件流上。 - 通过 `CopyTo` 方法将源文件流的数据复制到编码器流中,实现压缩。 - **解压缩部分**: - 打开压缩文件流和解压缩后文件流。 - 使用 `LZ4Stream.Decode` 方法创建一个解码器流,将其包装在压缩文件流上。 - 通过 `CopyTo` 方法将解码器流的数据复制到解压缩后文件流中,实现解压缩。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值