CodeForcesGym 100641B A Cure for the Common Code

A Cure for the Common Code

Time Limit: 3000ms
Memory Limit: 262144KB
This problem will be judged on CodeForcesGym. Original ID: 100641B
64-bit integer IO format: %I64d      Java class name: (Any)
 

You've been tasked with relaying coded messages to your fellow resistance ghters. Each coded message is a sequence of lower-case letters that you furtively scrawl on monuments in the dead of night. Since you're writing these messages by hand, the longer the message, the greater the likelihood of being caught by the evil empire while writing. Because of this you decide it would be worthwhile to come up with a simple encoding that might allow for shorter messages. After thinking about it for a while, you decide to use integers and parentheses to indicate repetition of substrings when doing so shortens the number of characters you need to write. For example, the 10 character string
abcbcbcbca could be more brie y written as the 7 character string a4(bc)a If a single letter is being repeated, parentheses are not needed. Also, repetitions may themselves be
repeated, so you can write the 20 character string abbbcdcdcdabbbcdcdcd

as the 11 character string


2(a3b3(cd))


and so forth.
Input Time Limit: 5 secs, No. of Test Cases: 39, Input File Size 2.95K
Each test case consists of a single line containing a string of lower-case letters of length  500. A line
containing a single 0 will terminate the input.


Output
For each test case, output the number of characters needed for a minimal encoding of the string.


Sample Input
abcbcbcbca
abbbcdcdcdabbbcdcdcd
0

Sample Output
Case 1: 7
Case 2: 11

解题:KMP预处理循环节+区间dp

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 510;
 4 const int INF = 0x3f3f3f3f;
 5 int fail[maxn][maxn];
 6 char str[maxn];
 7 void getFail(int st) {
 8     fail[st][st] = st-1;
 9     for(int i = st, j = st-1; str[i]; ++i) {
10         while(j != st-1 && str[i] != str[j]) j = fail[st][j];
11         fail[st][i + 1] = ++j;
12     }
13 }
14 int dp[maxn][maxn];
15 int calc(int x,int ret = 0) {
16     while(x) {
17         x /= 10;
18         ++ret;
19     }
20     return max(1,ret);
21 }
22 int main() {
23     int cs = 1;
24     while(~scanf("%s",str)) {
25         if(str[0] == '0') break;
26         int len = strlen(str);
27         for(int i = 0; i < len; ++i) {
28             getFail(i);
29             dp[i][i] = 1;
30         }
31         for(int i = 2; i <= len; ++i) {
32             for(int j = 0; j + i <= len; ++j) {
33                 int t = j + i - 1;
34                 dp[j][t] = INF;
35                 for(int k = j; k < t; ++k)
36                     dp[j][t] = min(dp[j][t],dp[j][k] + dp[k+1][t]);
37                 int cycle = i - fail[j][t + 1] + j;
38                 if(i >  cycle && cycle > 0 && i%cycle == 0) {
39                     int ret = dp[j][j + cycle-1] + calc(i/cycle);
40                     if(cycle > 1) ret += 2;
41                     dp[j][t] = min(dp[j][t],ret);
42                 }
43             }
44         }
45         printf("Case %d: %d
",cs++,dp[0][len-1]);
46     }
47     return 0;
48 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4758408.html