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. Fitnesse Page 简单使用

    more information- http://www.fitnesse.org/FitNesse.UserGuide 1.1   Edit 点击该按钮,则可以开始编辑(如果该按钮没有出现,则这个页 ...

  2. Eclipseproject出现红叉

    通过import导入包的时候往往会出现红叉的情况.但又没有错:这样的情况能够这样解决 1.选中项目.按Alt键和Enter键 2.选Andriod,在右側的版本号信息里选择一个不同的版本号,如原来默认 ...

  3. 点击TButton后的执行OnClick和OnMouseDown两个事件的过程(其实是通过WM_COMMAND执行程序员的代码)

    问题的来源:在李维的<深入浅出VCL>一书中提到了点击TButton会触发WM_COMMAND消息,正是它真正执行了程序员的代码.也许是我比较笨,没有理解他说的含义.但是后来经过追踪代码和 ...

  4. [NOIP 2017] 奶酪

    [题目链接] http://uoj.ac/problem/332 [算法] 直接搜索即可 注意使用long long [代码] #include<bits/stdc++.h> using ...

  5. 字符流、字节流、二进制及其在HTTP协议传输

    一.二进制.字节.字符流概念 字(Byte)节是长度单位.位(bit)也是长度单位.计算机通信和存储的时候都是以010101这样的二进制数据为基础的二进制数有两个特点:它由两个基本字符0,1组成,二进 ...

  6. ClassLoader.getResourceAsStream(name);获取配置文件的方法

    ClassLoader.getResourceAsStream(name);路径问题 InputStream in = getClass().getResourceAsStream('/'+" ...

  7. 根据日期获取,x岁x月x天

    c#: DateTime startDate = new DateTime(); DateTime endDate = new DateTime(); ; ; ; if (endDate.Month& ...

  8. 基于RHEL8/CentOS8的网络IP配置详解

    ➡ 在rhel8(含centos8)上,没有传统的network.service,在/etc/sysconfig/network-scripts/里也看不到任何脚本文件,那么该如何进行网络配置呢. ➡ ...

  9. Ssh safe

    新建用户,设置密码 useradd eason passwd eason 不允许root直接登陆 修改配置文件 vi /etc/ssh/sshd_config 禁止root登录 查找“#PermitR ...

  10. python3爬虫:下载网易云音乐排行榜

    #!/usr/bin/python3# -*- encoding:utf-8 -*- # 网易云音乐批量下载 import requestsimport urllib # 榜单歌曲批量下载# r = ...