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\n",cs++,dp[0][len-1]);
46     }
47     return 0;
48 }

时间: 2024-08-28 16:09:35

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

CSU1620: A Cure for the Common Code(KMP+区间DP)

Description Input Output Sample Input abcbcbcbca abbbcdcdcdabbbcdcdcd 0 Sample Output Case 1: 7 Case 2: 11 HINT Source 题意:把字符串简化,问简化得到的最短长度是多少 思路:要简化首先要求循环节,这里用kmp解决,而要求所有简化中最短的的话,用区间dp可以求得 <pre name="code" class="cpp">#include &

CSU 1620: A Cure for the Common Code (区间DP KMP预处理)

链接 : http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1620 题意 : 给一个字符串 问怎么压缩字符串使得最终个数最小 具体怎么压缩请参照图示 很好明白. 题目就是需要找到 对于每个后缀 看成一个新字符串 找出它的前缀的最小循环节. 过程和大白书 P213页 是一样的,只需要对每个后缀跑一遍KMP求出周期. 剩下的过程就是区间DP了. dp[i][j] = min ( dp[i][k], dp[k+1][j] ); 如果i - j这一段存在

CSU 1620: A Cure for the Common Code(KMP+区间DP)

Description Input Output Sample Input abcbcbcbca abbbcdcdcdabbbcdcdcd 0 Sample Output Case 1: 7 Case 2: 11 HINT 题意:给一个小写子母的串,相邻相同的子串可以合在一起,问这个串合并后最短可得多长. 解题:KMP & 区间DP #include<stdio.h> #include<string.h> const int N = 505; int next1[N]; c

The way of Webpack learning (II.) -- Extract common code(多页面提取公共代码)

学习之路基于webpack3.10.0,webpack4.0之后更新. 多页面提取公共代码!!! 一:文件关系 pageA --> subA.subB --> moduleA pageB --> subA.subB --> moduleA 那么pageA.pageB 的公共代码就是subA.subB .moduleA. 二:webpack.config.js文件配置 var webpack = require('webpack') var path = require('path'

xcode armv6 armv7 armv7s arm64指令集

郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 如果文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额随意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源码下载:点我传送 之前每个版本的ipa包打出来都才20M出头,后来不知道从哪个版本起打的包每次都将近40M,但是工程中也没有明显的增加很大第三方类或者图片.静态文件.于是就开始了各种瘦身计划:压缩图片,删除不必要静态库.做了一番改动后发现包只比原来小了1M多,收效甚微. 其实是armv6 armv7 armv7s arm64引起

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

在写代码的时候,发现从父类class通过getDeclaredMethod获取的Method可以调用子类的对象,而子类改写了这个方法,从子类class通过getDeclaredMethod也能获取到Method,这时去调用父类的对象也会报错.虽然这是很符合多态的现象,也符合java的动态绑定规范,但还是想弄懂java是如何实现的,就学习了下Method的源代码. Method的invoke方法 1.先检查 AccessibleObject的override属性是否为true. Accessibl

策略模式(Strategy Method)

策略模式可以看做“可插入式算法(Pluggable)”,将子类各自的行为和公共的逻辑分离开来,将子类的行为抽象为算法插入到公共的逻辑中,这样替换子类的行为也不会对公共逻辑产生影响,也不会影响到调用类的逻辑. 下面是一个策略模式的简单例子,类图如下: 公共逻辑Context的代码如下: public class Context{ public void contextInterface(){ //add common code here strategy.strategyInterface();

YASM User Manual

This document is the user manual for the Yasm assembler. It is intended as both an introduction and a general-purpose reference for all Yasm users. 1.?Introduction Yasm is a BSD-licensed assembler that is designed from the ground up to allow for mult

服务器上的文件的上传和下载

我们有很多种方式上传文件到服务器上,比如FTP, 比如使用命令行工具:SZ/RZ, SCP等等的. 我之前经常使用的是SZ/RZ这个命令,但是在Mac下经常乱码然后卡住不动了,所以我很崩溃.下面说说我现在 采用的办法吧. 下载文件 python -m SimpleHTTPServer 9999 上传文件 文件名:SimpleHTTPServerWithUpload.py #!/usr/bin/env python """Simple HTTP Server With Uplo