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

 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
int fail[maxn][maxn];
char str[maxn];
void getFail(int st) {
fail[st][st] = st-;
for(int i = st, j = st-; str[i]; ++i) {
while(j != st- && str[i] != str[j]) j = fail[st][j];
fail[st][i + ] = ++j;
}
}
int dp[maxn][maxn];
int calc(int x,int ret = ) {
while(x) {
x /= ;
++ret;
}
return max(,ret);
}
int main() {
int cs = ;
while(~scanf("%s",str)) {
if(str[] == '') break;
int len = strlen(str);
for(int i = ; i < len; ++i) {
getFail(i);
dp[i][i] = ;
}
for(int i = ; i <= len; ++i) {
for(int j = ; j + i <= len; ++j) {
int t = j + i - ;
dp[j][t] = INF;
for(int k = j; k < t; ++k)
dp[j][t] = min(dp[j][t],dp[j][k] + dp[k+][t]);
int cycle = i - fail[j][t + ] + j;
if(i > cycle && cycle > && i%cycle == ) {
int ret = dp[j][j + cycle-] + calc(i/cycle);
if(cycle > ) ret += ;
dp[j][t] = min(dp[j][t],ret);
}
}
}
printf("Case %d: %d\n",cs++,dp[][len-]);
}
return ;
}

最新文章

  1. git clone错误
  2. C++builder中使用TScrollBox 以后,让scrollBox相应鼠标的上下滑动
  3. 使用Google产品以来遇到的最糟糕、最霸道、最让人抓狂的设计
  4. 网页闯关游戏(riddle webgame)--游戏玩法和整体介绍
  5. Android 程序打包和安装过程
  6. Java--接口和类集框架
  7. sql server对并发的处理-乐观锁和悲观锁【粘】
  8. HDU 4746 (莫比乌斯反演) Mophues
  9. Nginx环境下配置PHP使用的SSL认证(https)
  10. 点点滴滴-ConfigurationManager.AppSettings
  11. java学习常遇问题及解决方案
  12. 关于jquery全选反选 批量删除的一点心得
  13. 如何基于 eolinker 的进行接口管理
  14. 实时同步rsync+inotify
  15. DAY02、正式介绍python
  16. mysql的日志及利用mysqldump备份及还原
  17. 【Hadoop学习之七】Hadoop YARN
  18. 【PowerDesigner】【3】字段添加注释和默认值
  19. PHP编程时的规范化命名
  20. java指针与引用(转载)

热门文章

  1. Spring注解@RequestMapping请求路径映射问题
  2. SICP 习题1.16-1.19体会
  3. JavaScript大数组如何根据对象的key快速找到并删除
  4. 【Codeforces 258D】 Count Good Substrings
  5. 备份SQL SERVER 2005数据库
  6. ubantu下NiFi单节点安装部署
  7. BZOJ 4547 矩阵快速幂
  8. B - Guess a number!
  9. Sybase 动态改变存储过程里查询的数据库
  10. hdu4081 Qin Shi Huang&#39;s National Road System 次小生成树