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. [iOS]UITableViewController完毕收回键盘操作

    UITableViewController 本身可以实现键盘适配(cell中控件焦点会移动到键盘上方 在做键盘收回的时候思考过例如以下方案 1.tableview加入点击事件 结果:点击事件和tabl ...

  2. LLVM每日谈之十九 LLVM的第一本系统的书&lt;Getting Started with LLVM Core Libraries&gt;

    作者:史宁宁(snsn1984) LLVM最终有了一本系统的书了--<Getting Started with LLVM Core Libraries>. 这本书号称是LLVM的第一本书, ...

  3. int、bigint、smallint 和 tinyint范围

    int.bigint.smallint 和 tinyint范围使用整数数据的精确数字数据类型.bigint从 -2^63 (-9223372036854775808) 到 2^63-1 (922337 ...

  4. Running the app on your device

    So far, you've run the app on the Simulator. That's nice and all but probably notwhy you're learning ...

  5. B1260 [CQOI2007]涂色paint 区间dp

    这个题和我一开始想的区别不是很大,但是要我独自做出来还是有一些难度. 每一次涂色 只有这两种可能: 1) 把一段未被 覆盖过的区间 涂成 * 色 2) 把一段被一种颜色覆盖的区间涂成 * 色 (并且 ...

  6. 2017-3-10 leetcode 229 238 268

    今天登陆leetcode突然发现531被锁了,有种占了便宜的感觉哈哈哈! ================================================ leetcode229 Ma ...

  7. 6.11Realm简介

    CasRealm 统一认证授权中心  跟单点登录有关的.IniRealm 静态文件

  8. day63-webservice 06.在web项目中发布以类的形式发布webservice

    真正用的时候都是需要部署在WEB服务器里面. 不能写主函数来发布了,需要借助于我们WEB. 4.配置web.xml, <!DOCTYPE web-app PUBLIC "-//Sun ...

  9. day63-webservice 04.JaxWsServerFactoryBean和SOAP1.2

    <wsdl:definitions xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap12="h ...

  10. php gd

    imagecopy() 函数用于拷贝图像或图像的一部分. imagecopyresized() 函数用于拷贝部分图像并调整大小. imagecopy() imagecopy() 函数用于拷贝图像或图像 ...