您的位置:首页 > 其它

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

2013-08-28 19:32 423 查看
M - String CompressionCrawling in process...Crawling failedTime Limit:3000MSMemory Limit:0KB 64bit IO Format:%lld & %lluSubmitStatusAppoint description:System Crawler (2013-06-01)DescriptionRun 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 substringS is repeated k times, replacek copies of S byk(S) . For example, letsgogogo is compressed intolets3(go). The length of letsgogogo is 10, and the length oflets3(go) is 9. In general, the length of k(S) is (number of digits ink ) + (length of S ) + 2 (for `(' and `)'). For example, the length of123(abc) is 8. It is also possible to nest compression, so the substringS may itself be a compressed string. For example,nowletsgogogoletsgogogo could be compressed as a now2(lets3(go)), andnowletsgogogoletsgogogoandrunrunrun 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.InputYour program is to read from standard input. The input consists ofT 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.OutputYour 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]);}}}
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: