UVALive 3363 String Compression (区间DP,4级)
Description
![Download as PDF Download as PDF](https://icpcarchive.ecs.baylor.edu/components/com_onlinejudge/images/button_pdf.png)
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]); } } }