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 ;
}

CodeForcesGym 100641B A Cure for the Common Code的更多相关文章

  1. C# Common Code

    DatePicker 控件日期格式化,可以在App.xaml.cs中添加下面代码 方法一 不推荐: Thread.CurrentThread.CurrentCulture = (CultureInfo ...

  2. CV code references

    转:http://www.sigvc.org/bbs/thread-72-1-1.html 一.特征提取Feature Extraction:   SIFT [1] [Demo program][SI ...

  3. Integrate Your Code with the Frameworks---整合你的代码和框架

    Back to Frameworks Integrate Your Code with the Frameworks When you develop an app for OS X or iOS, ...

  4. Separate code and data contexts: an architectural approach to virtual text sharing

    The present invention provides a processor including a core unit for processing requests from at lea ...

  5. JMM(java内存模型)

    What is a memory model, anyway? In multiprocessorsystems, processors generally have one or more laye ...

  6. python简单搭建HTTP Web服务器

    对于Python 2,简单搭建Web服务器,只需在i需要搭建Web服务器的目录(如C:/ 或 /home/klchang/)下,输入如下命令: python -m SimpleHTTPServer 8 ...

  7. JAVA深入研究——Method的Invoke方法。

    在写代码的时候,发现Method可以调用子类的对象,但子类即使是改写了的Method,方法名一样,去调用父类的对象也会报错,虽然这是很符合多态的现象,也符合java的动态绑定规范,但还是想弄懂java ...

  8. http2协议翻译(转)

    超文本传输协议版本 2 IETF HTTP2草案(draft-ietf-httpbis-http2-13) 摘要 本规范描述了一种优化的超文本传输协议(HTTP).HTTP/2通过引进报头字段压缩以及 ...

  9. Android按键事件处理流程 -- KeyEvent

    刚接触Android开发的时候,对touch.key事件的处理总是一知半解,一会是Activity里的方法,一会是各种View 中的,自己始终不清楚到底哪个在先哪个在后,总之对整个处理流程没能很好的把 ...

随机推荐

  1. 【POJ 2486】 Apple Tree(树型dp)

    [POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8981   Acce ...

  2. TestNG环境搭建以及框架初识

    TestNG的英文为Test Next Generation, 听上去好像下一代测试框架已经无法正常命名了的样子,哈哈,言归正传,啥是TestNG呢,它是一套测试框架,在原来的Junit框架的思想基础 ...

  3. php word转pdf

    我们知道,PHP语言的合理运用可以帮助我们实现导出Word文档的功能.今天我们将为大家介绍PHP处理Word转PDF的相关实现方法. PHP关键字this指向当前对象指针 PHP self关键字指向类 ...

  4. yii依赖注入

    为了降低代码耦合程度,提高项目的可维护性,Yii采用多许多当下最流行又相对成熟的设计模式,包括了依赖注入(Denpdency Injection, DI)和服务定位器(Service Locator) ...

  5. anaconda 使用 及 tensorflow-gpu 安装

    Anaconda简易使用 创建新环境 conda create -n rcnn python=3.6 删除环境 conda remove -n rcnn --all 进入环境 conda activa ...

  6. Oracle---显式游标

    一  游标的分类 在Oracle中提供了两种类型的游标:静态游标和动态游标. 1.静态游标是在编译时知道其SELECT语句的游标.静态游标又分为两种类型,即隐式游标和显式游标. 2.当用户需要为游标使 ...

  7. 了解jQuery的$符号

    $是什么? 可以使用typeof关键字来观察$的本质. console.log(type of $); //输出结果为function 因此可以得出结论,$其实就是一个函数.$(); 只是根据所给参数 ...

  8. PostgreSQL的HA解决方案-项目概述

    公司使用的数据库时postgresql,一直运行都很流畅,但是最近java新做的管理平台,由于登录用户较多,并发性比较大.另外新系统可能优化也存在问题,所以pg经常崩溃,所以我就开始研究如何事项pg的 ...

  9. springmvc 中配置aop

    之前自己搭建了springmvc+spring+mybaits/hibernate 的框架,并在applicationcontext.xml中配置了aop,但 发现aop根本不生效,而不用框架的话则可 ...

  10. (转)基于MVC4+EasyUI的Web开发框架经验总结(1)-利用jQuery Tags Input 插件显示选择记录

    http://www.cnblogs.com/wuhuacong/p/3667703.html 最近花了不少时间在重构和进一步提炼我的Web开发框架上,力求在用户体验和界面设计方面,和Winform开 ...