[Andrew Stankevich's Contest#21] Lempel-Ziv Compression
Problem Description
Most modern archivers, such as WinRAR or WinZIP, use modifications of Lempel-Ziv method as their primary compression algorithm. Although decompression of LZ-compressed archives is usually easy and fast, the compression process itself is often rather complicated and slow. Therefore professional archivers use approximation methods that sometimes do not allow to achieve the best possible compression.
This situation doesn’t satisfy your chief George. He would like to create the best archiver WinGOR. The archiver will use the following modification of LZ77 algorithm.
The text is partitioned to chunks of length not exceeding 4096. Each chunk is compressed independently. We will describe the decompression of one chunk t. Based on this description, you will have to create a compression algorithm that will create the shortest possible compressed chunk x from the given chunk t.
The compressed chunk is written down as the sequence of plain characters and repetition blocks. Plain character is 8 bits long. When decompressing, plain character c is simply copied to output. Repetition block (r; l) consists of two parts: reference r and length l, each 12 bits long. Reference r is an integer number between 1 and 4095. When repetition block (r; l) is obtained after decompressing i − 1 characters of text, characters t[i − r ... i − r + l − 1] are copied to output. Note, that r can be less than l, in this
case recently copied characters are copied to output as well.
To help decompressor distinguish between plain characters and repetition blocks a leading bit is prepended to each element of the compressed text: 0 means plain character follows, 1 — repetition block follows.
For example, “aaabbaaabababababab” can be compressed as “aaabb(5,4)(2,10)”. The compressed variant has 8 + 8 + 8 + 8 + 8 + 24 + 24 + 7 = 95 bits instead of 152 in the original text (additional 7 bits are used to distinguish between plain characters and repetition blocks).
Given a text chunk, find its compressed representation which needs fewest number of bits to encode
Input
Output
Sample Input
aaabbaaabababababab
Sample Output
95
aaabb(5,4)(2,10)
题意概述
分析
这里需要的一点小trick在于如何寻找尽量靠前的一段最长重复子串。代入kmp算法的思路,我们可以对原字符串的每一段后缀预处理出next数组,这样原字符串中每个前缀的最长重复子串就可以在O(n)的时间内得到了。于是这里可以得出一个复杂度O(n^2)的动态规划:
记minbit[i]为压缩前i个字符所需的最小数据量,str[i]为此时的决策,r[i],l[i]为这次转移时压缩成的数对(若不需压缩,str[i]=i, r[i]储存不需压缩的部分的起始点)
那么,每次转移时就可以从0到i-1枚举决策j,选择可以使(minbit[j] + (i-j)*9)或minbit[i - next[j][i-1]] + 25取得最小值的j,并保存决策。最后可以通过一个递归函数推出最终答案。
(这里需要注意一点问题……next数组是二维的,数据范围maxn为2^12,而内存限制为64000KB……如果用next[maxn][maxn]的方式开数组明显会MLE……所以我鼓起勇气采用了动态内存分配……)
AC代码
1 //Verdict: Accepted 2 // Submission Date: -- ::
3 // Time: 8256MS
4 // Memory: 34624KB
5
6 /*=============================================================================================================================*/
7 /*======================================================Code by Asm.Def========================================================*/
8 /*=============================================================================================================================*/
9 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <memory.h>
#include <cstring>
#include <cstdlib>
using namespace std;
#define maxn ((int)4.1e3)
/*===========================================================TYPES=============================================================*/
typedef long long LL;
/*======================================================GLOBAL VARIABLES=======================================================*/
char ch[maxn];
int len = , minbit[maxn], *next[maxn];
int l[maxn], r[maxn], str[maxn];
/*==========================================================FUNCTIONS==========================================================*/
inline void getnext(int l){
int i, j, L = len - l;
next[l] = new int[L+];
int *Next = next[l];
Next[] = ;
for(i = ;i < L;++i){
j = Next[i-] - ;
while(ch[l+i] != ch[l+j+] && j >= )
j = Next[j] - ;
if(ch[l+i] == ch[l+j+])
Next[i] = j + ;
else Next[i] = ;
}
}
void printpro(int i){
if(str[i] == i){
if(r[i])printpro(r[i]-);
int j;
for(j = r[i];j <= i;++j)putchar(ch[j]);
return;
}
printpro(str[i]);
printf("(%d,%d)", r[i], l[i]);
}
int main(){
#ifdef DEBUG
assert(freopen("test","r",stdin));
#endif
//--------------------------------------------------variables-----------------------------------------------------------
//-----------------------------------------------------work-------------------------------------------------------------
char c;
while(isalpha(c = getchar()))str[len] = len, ch[len++] = c;
int i, j, Min, t;
for(i = ;i < len - ; ++i)
getnext(i);
minbit[] = ;
for(i = ;i < len; ++i){
Min = 0x7fffffff;
for(j = ;j < i;++j)
if(minbit[j] + (i-j)* < Min){
Min = minbit[j] + (i-j)*;
str[i] = i;
r[i] = j+;
}
for(j = ;j < i; ++j){
t = next[j][i-j];
if(!t)continue;
if(minbit[i-t] + < Min){
Min = minbit[i-t] + ;
str[i] = i-t;
r[i] = i+-t-j;
l[i] = t;
}
}
minbit[i] = Min;
}
printf("%d\n", minbit[len-]);
printpro(len-);
return ;
}
/*=============================================================================================================================*/
[Andrew Stankevich's Contest#21] Lempel-Ziv Compression的更多相关文章
- Andrew Stankevich's Contest (21) J dp+组合数
坑爹的,,组合数模板,,, 6132 njczy2010 1412 Accepted 5572 MS 50620 KB C++ 1844 B 2014-10-02 21:41:15 J - 2-3 T ...
- 【模拟ACM排名】ZOJ-2593 Ranking (Andrew Stankevich’s Contest #5)
真心是道水题,但找bug找的我想剁手了/(ㄒoㄒ)/~~ 注意几个坑点, 1.输入,getline(cin); / gets(); 一行输入,注意前面要加getchar(); 输入运行记录的时候可 ...
- Andrew Stankevich's Contest (1)
Andrew Stankevich's Contest (1) 打一半出门了,回来才补完了...各种大数又不能上java..也是蛋疼无比 A:依据置换循环节非常easy得出要gcd(x, n) = 1 ...
- acdream:Andrew Stankevich Contest 3:Two Cylinders:数值积分
Two Cylinders Special JudgeTime Limit: 10000/5000MS (Java/Others)Memory Limit: 128000/64000KB (Java/ ...
- GYM 100608G 记忆化搜索+概率 2014-2015 Winter Petrozavodsk Camp, Andrew Stankevich Contest 47 (ASC 47)
https://codeforces.com/gym/100608 题意: 两个人玩游戏,每个人有一个长为d的b进制数字,两个人轮流摇一个$[0,b-1]$的骰子,并将选出的数字填入自己的d个空位之中 ...
- LeetCode Weekly Contest 21
1. 530. Minimum Absolute Difference in BST 最小的差一定发生在有序数组的相邻两个数之间,所以对每一个数,找他的前驱和后继,更新结果即可!再仔细一想,bst的中 ...
- NOIP模拟·20141105题解
[A.韩信点兵] 结论题+模板题,用到了中国剩余定理,维基百科上讲的就比较详细,这里就不再赘述了…… 对于这题,我们先利用中国剩余定理($x \equiv \sum{(a_i m_i (m_i^{-1 ...
- ZJU 2605 Under Control
Under Control Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ...
- AC自动机-算法详解
What's Aho-Corasick automaton? 一种多模式串匹配算法,该算法在1975年产生于贝尔实验室,是著名的多模式匹配算法之一. 简单的说,KMP用来在一篇文章中匹配一个模式串:但 ...
随机推荐
- DirectX介绍(转)
原文转自 https://baike.baidu.com/item/Direct3D/910353
- DTW 算法(转)
DTW为(Dynamic Time Warping,动态时间归准)的简称.应用很广,主要是在模板匹配中,比如说用在孤立词语音识别,计算机视觉中的行为识别,信息检索等中.可能大家学过这些类似的课程都看到 ...
- java===java基础学习(12)---方法的重写和重载
覆盖 / 重写(Override) 重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变.即外壳不变,核心重写! 重写的好处在于子类可以根据需要,定义特定于自己的行为. 也 ...
- openssl-0.9.8y
openssl-0.9.8y 支持 32位和64位 编译不报错和向上兼容和向下兼容. http://www.openssl.org/source/openssl-0.9.8y.tar.gz https ...
- 【bzoj4033】HAOI2015树上染色
树形dp. #include<bits/stdc++.h> #define N 2010 using namespace std; typedef long long ll; ,head[ ...
- FineReport——弹出新窗体选值并回调
主要实现的功能: 在主页面,通过单击按钮,弹出窗体,在窗体中通过下拉框选择值并查询,如果是多值,可以通过复选框选择,点击确定,将选中的行的字段值传递给主页面的下拉复选框,定义其编辑后事件进行查询.将想 ...
- thinkphp5 消息队列thinkphp-queue扩展
1.简介 thinkphp-queue是thinkphp的一个第三方扩展, 内置了 Redis,Database,Topthink ,Sync这四种驱动,推荐使用redis 2. 下载 和安装 com ...
- 利用getBoundingClientRect()来实现div容器滚动固定
ele.getBoundingClientRect()的方法是可以获得一个元素在整个视图窗口的位置 可以return的值有width,height,top,left,x,y,right,bottom ...
- http跟https的区别
http: Hypertext transform protocol 超文本传输协议 是一个为了传输超媒体文档(比如html)的应用层协议 是为了web的浏览器跟web的server端的交流而设计的, ...
- JMeter------ _time 函数的使用(时间戳、当前时间)
操作步骤: 1.通过函数助手,生成一个_time 函数: 2.如果参数为时间戳,那公式为: ${__time(,)} : 默认该公式精确到毫秒级别, 13位数 ${__time(/1000,)} ...