String Compression - UVa 1351 dp+hash
2014-11-15 12:59
155 查看
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 Sis 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 asnow2(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. 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.
The following shows sample input and output for four test cases.
题意:将字符串折叠,求出最后字符数的最小值。
思路:对于l到r的字符串一共有两种折叠方式,一种是dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+1][r]);第二种是l到r折叠多少次。
AC代码如下:
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 Sis 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 asnow2(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 theinput. 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
题意:将字符串折叠,求出最后字符数的最小值。
思路:对于l到r的字符串一共有两种折叠方式,一种是dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+1][r]);第二种是l到r折叠多少次。
AC代码如下:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef unsigned long long ll; char s[210]; int dp[210][210],f[210][210],MOD=1e9+7; ll pow[210],Hash[210]; int wei(int S) { int ret=0; while(S) { ret++; S/=10; } return ret; } bool check(int l,int r,int k) { int i,j,maxn=(r-l+1)/k,l2=l,r2=l+k-1,l3,r3; ll ret=Hash[r2]-Hash[l2-1]; for(i=1;i<maxn;i++) { l3=l+k*i; r3=l+k-1+k*i; if(ret*pow[k*i]!=Hash[r3]-Hash[l3-1]) return false; } return true; } void solve(int l,int r) { int i,j,k,len=r-l+1; dp[l][r]=MOD; for(k=l;k<r;k++) dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+1][r]); for(k=1;k<len;k++) if((len)%k==0 && check(l,r,k)) dp[l][r]=min(dp[l][r],dp[l][l+k-1]+2+wei(len/k)); } int main() { int t,T,n,i,j,k; pow[0]=1; for(i=1;i<=205;i++) pow[i]=pow[i-1]*MOD; scanf("%d",&T); for(t=1;t<=T;t++) { scanf("%s",s+1); n=strlen(s+1); for(i=1;i<=n;i++) Hash[i]=Hash[i-1]+s[i]*pow[i]; for(i=1;i<=n;i++) dp[i][i]=1; for(k=1;k<=n;k++) for(i=1;i+k<=n;i++) solve(i,i+k); printf("%d\n",dp[1] ); } }
相关文章推荐
- UVA - 1390(概率dp + hash)
- UVa 10029 Edit Step Ladders(hash状态压缩DP)
- uva 1351 - String Compression(dp)
- uva 1351 - String Compression(区间DP,好题,较难)
- UVA1351-----String Compression-----区间DP(记忆化搜索实现)
- UVA 1351 String Compression(区间DP)
- 解题报告(二):uva 10029 - Edit Step Ladders(dp+hash)
- UVA 1351 - String Compression(区间DP)
- uva 10029 HASH + DP
- UVa 10029 hash + dp
- 区间DP UVA 1351 String Compression
- uva 1351 区间dp
- UVA1351-----String Compression-----区间DP(记忆化搜索实现)
- uva1351 - String Compression 区间DP
- uva 1351 - String Compression(区间DP)
- uva1351 dp
- uva 1351 String Compression(字符串区间dp)
- UVA11270 轮廓线DP
- UVa 11400 - Lighting System Design(DP)
- uva 10003 Cutting Sticks 【区间dp】