Compress string time limit: 2000 MS | memory limit: 65535 kb difficulty: 3
-
Description
-
One day, a beautiful girl ask lyh to help her complete a complicated task-using a new compression method similar to Run Length Encoding (RLE) compress a string. though the task is difficult, lyh is gglad to help her. the compress rules of the new method is as follows: if a substring S is repeated K times, replace K copies of S by K (s ). for example,
LetsgogogoIs compressed
Lets3 (GO). The length
LetsgogogoIs 10, and the length
Lets3 (GO)Is 9. In general, the length of K (s) is (number of digits in K) + (length of S) + 2. For example, the length
123 (ABC)Is 8. It is also possible that substring S is a compressed string. For example
NowletsgogogoletsgogogoandrunrunrunCocould be compressed
Now2 (lets3 (GO) and3 (run). In order to make the girl happy, lyh solved the task in a short time. Can you solve it?
-
Input
-
Thera are multiple test cases.
Each test case contains a string, the length of the string is no more than 200, all the character is lower case alphabet.
-
Output
-
For each test case, print the length of the shortest compressed string.
-
Sample Input
-
ababcd letsgogogonowletsgogogoletsgogogoandrunrunrun
-
Sample output
-
6924
-
A string of no more than 200 characters can be compressed according to certain rules, that is, several consecutive identical substrings can be compressed into one string, for example, letsgogogo can be compressed to lets3 (GO), and the compressed substring can be compressed if it can be further compressed. For example, nowletsgogogogoletsgogogoandrunrunrun can be compressed to now2 (lets3 (GO )) and3 (run ). Ask the minimum length of the compressed string.
-
Analysis: The interval DP, DP [I] [J] indicates the shortest length of the string from I to J.
DP [I] [J] = min (DP [I] [J], DP [I] [k] + dp [k + 1] [J]).
Then, judge whether the current substring can be compressed, that is, whether it is composed of repeated strings. When judging, you only need to forcibly enumerate the repeated length to determine.
If the current substring can be compressed, DP [I] [J] = min (DP [I] [J], DP [I] [I + len-1] + 2 + digcount (J-I + 1)/Len ));,
Note that if it is a number, use the number of digits to indicate the number to be increased, rather than simply increasing 1.
-
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 210;#define INF 0x3fffffffchar str[N];int n, dp[N][N];int digit_cnt(int x){ int a = 0; while(x) { a++; x /= 10; } return a;}bool check(int l, int r, int len){ if((r - l + 1) % len) return false; for(int i = l; i < l + len; i++) { for(int j = i + len; j <= r; j += len) if(str[i] != str[j]) return false; } return true;}int get_ans(){ int i, j, k; n = strlen(str+1); for(i = 1; i <= n; i++) dp[i][i] = 1; for(i = n - 1; i >= 1; i--) { for(j = i + 1; j <= n; j++) { dp[i][j] = INF; for(k = i; k < j; k++) dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j]); for(int len = 1; len <= j-i+1; len++) { if(check(i, j, len)) { dp[i][j] = min(dp[i][j], dp[i][i+len-1] + 2 + digit_cnt((j - i + 1) / len)); } } } } return dp[1][n];}int main(){ while(~scanf("%s", str+1)) { printf("%d\n", get_ans()); } return 0;}