Front compression

翻译见后

Description

Front compression is a type of delta encoding compression algorithm whereby common prefixes and their lengths are recorded so that they need not be duplicated. For example:

The size of the input is 43 bytes, while the size of the compressed output is 40. Here, every space and newline is also counted as 1 byte.
Given the input, each line of which is a substring of a long string, what are sizes of it and corresponding compressed output?

Input

There are multiple test cases. Process to the End of File.
The first line of each test case is a long string S made up of lowercase letters, whose length doesn’t exceed 100,000. The second line contains a integer 1 ≤ N ≤ 100,000, which is the number of lines in the input. Each of the following N lines contains two integers 0 ≤ A < B ≤ length(S), indicating that that line of the input is substring [A, B) of S.

Output

For each test case, output the sizes of the input and corresponding compressed output.

Sample Input

frcode
2
0 6
0 6
unitedstatesofamerica
3
0 6
0 12
0 21
myxophytamyxopodnabnabbednabbingnabit
6
0 9
9 16
16 19
19 25
25 32
32 37

Sample Output

14 12
42 31
43 40

翻译:

题意:
根据图中的方法压缩输入的字符串,压缩后为可匹配的长度空格与剩余的字符串,输出未压缩字符串的长度与压缩后的长度,空格与提行也算。

我们来模拟一个样例:
frcode
2
0 6
0 6
则第一个输入串为frcode,第二个也为frcode,原串为
frcode
frcode
它的压缩后结果为
0 frcode
6
于是它的原长为14,压缩后为12。

那么我们发现原长十分好算,即每次的长度相加即可,而压缩后的长度就可以有后缀数组来解决(这个题数据水,暴力也能过)。最后注意一下精度就AC~(≧▽≦)/~啦啦啦。

附代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cctype>
#include <algorithm>
#define clr(x) memset(x,0,sizeof(x))
#define LL long long
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endifusing namespace std;const int maxn = 100010;
int newline,str[maxn];
int n,m,len;
LL bf,la;
int t[maxn],t2[maxn],c[maxn],height[maxn],sa[maxn],rk[maxn],dp[maxn][20];
char s[maxn];void build() {int *x = t, *y = t2; m = 27;for(int i = 0; i < m; i++) c[i] = 0;for(int i = 0; i < n; i++) ++c[x[i] = str[i]];for(int i = 1; i < m; i++) c[i] += c[i-1];for(int i = n-1; i >= 0; i--) sa[--c[x[i]]] = i;for(int k = 1; k <= n; k <<= 1) {int p = 0;for(int i=n-k; i<n; ++i) y[p++]=i;for(int i=0; i<n; ++i) if(k<=sa[i]) y[p++]=sa[i]-k;for(int i = 0; i < m; ++i) c[i] = 0;for(int i = 0; i < n; ++i) ++c[x[y[i]]];for(int i = 1; i < m; ++i) c[i]+=c[i-1];for(int i = n-1; i >= 0; --i) sa[--c[x[y[i]]]] = y[i];swap(x, y); p = 1; x[sa[0]] = 0;for(int i = 1; i < n; i++)x[sa[i]] = y[sa[i]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k] ? p-1 : p++;  if(p >= n) break;m = p;}
}void Height() {for(int i = 0; i <= len; i++) rk[sa[i]] = i;for(int i = 0, k = 0; i < len; i++) {if(k) --k;if(rk[i] < 1) continue;int j = sa[rk[i]-1];while(str[i+k] == str[j+k]) k++;height[rk[i]] = k;}
}void ST() {for(int i = 1; i <= len; i++) dp[i][0] = height[i];for(int j = 1; (1<<j) <= len; j++)for(int i = 1; i+(1<<j)-1 <= len; i++)dp[i][j] = min(dp[i][j-1], dp[i+(1<<(j-1))][j-1]);
}int RMQ(int L,int R) {int k = 0;while((1<<(k+1)) <= R-L+1) k++;  return  min(dp[L][k], dp[R-(1<<k)+1][k]);
}int lcp(int a, int l) {if(a == l) return len-a;int u = rk[a], v = rk[l];if(u > v) return RMQ(v+1, u);else return RMQ(u+1, v);
}int cal(int p) {int v = p;for(int i = 1; ; i++) if(!(v/10)) return i; else v /= 10;
}int main() {while(scanf("%s",s) != EOF) {  len = strlen(s);for(int i = 0; i < len; i++) str[i] = s[i]-'a'+1;str[len] = 0; n = len+1;build(); Height(); ST();int a,b,l,r;scanf("%d%d%d",&newline,&a,&b);bf = b-a+1; la = b-a+3; newline--;while(newline--) {  scanf("%d%d",&l,&r); bf += r-l+1;  int p = min(lcp(a, l), min(b-a, r-l));  la += (r-l)-p+2+cal(p); a = l; b = r;}printf(AUTO" "AUTO"\n",bf,la);}  return 0;
}

【HDU4691】【后缀数组】Front compression 题解相关推荐

  1. FJUT3703 这还是一道数论题(二分 + hash + manacher 或者 STL + hash 或者 后缀数组 + hash)题解...

    Problem Description 最后来个字符串签个到吧,这题其实并不难,所需的算法比较基础,甚至你们最近还上过课. 为了降低难度,免得所有人爆零.这里给几个提示的关键字 :字符串,回文,二分, ...

  2. bzoj5108 数据_【Luogu5108】仰望半月的夜空(后缀数组)

    [Luogu5108]仰望半月的夜空(后缀数组) 题面 题解 实名举报这题在比赛之前还不是这个样子的,还被我用SAM给水过去了 很明显求出$SA$之后就是按照$SA$的顺序从前往后考虑每一个长度,这样 ...

  3. HDOJ 4691 Front compression 后缀数组

    后缀数组求两子串间的最大公共前缀. Front compression Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 102400/1 ...

  4. luoguP5108 仰望半月的夜空 [官方?]题解 后缀数组 / 后缀树 / 后缀自动机 + 线段树 / st表 + 二分...

    仰望半月的夜空 题解 可以的话,支持一下原作吧... 这道题数据很弱..... 因此各种乱搞估计都是能过的.... 算法一 暴力长度然后判断判断,复杂度\(O(n^3)\) 期望得分15分 算法二 通 ...

  5. 【2012百度之星/资格赛】H:用户请求中的品牌 [后缀数组]

    时间限制: 1000ms 内存限制: 65536kB 描述 馅饼同学是一个在百度工作,做用户请求(query)分析的同学,他在用户请求中经常会遇到一些很奇葩的词汇.在比方说"johnsonj ...

  6. 【bzoj4698】[Sdoi2008] Sandy的卡片 后缀数组

    题目描述 Sandy和Sue的热衷于收集干脆面中的卡片.然而,Sue收集卡片是因为卡片上漂亮的人物形象,而Sandy则是为了积攒卡片兑换超炫的人物模型.每一张卡片都由一些数字进行标记,第i张卡片的序列 ...

  7. 【uva10829-求形如UVU的串的个数】后缀数组+rmq or 直接for水过

    题意:UVU形式的串的个数,V的长度规定,U要一样,位置不同即为不同字串 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&am ...

  8. SPOJ - PHRASES Relevant Phrases of Annihilation —— 后缀数组 出现于所有字符串中两次且不重叠的最长公共子串...

    题目链接:https://vjudge.net/problem/SPOJ-PHRASES PHRASES - Relevant Phrases of Annihilation no tags  You ...

  9. 【bzoj3879】SvT 后缀数组+倍增RMQ+单调栈

    题目描述 (我并不想告诉你题目名字是什么鬼) 有一个长度为n的仅包含小写字母的字符串S,下标范围为[1,n]. 现在有若干组询问,对于每一个询问,我们给出若干个后缀(以其在S中出现的起始位置来表示), ...

最新文章

  1. 2017-2021年中国大数据产业预测分析及全球市场规模预测
  2. [导入]创建笔 (Visual C#)
  3. 计算机体系结构---第二章---指令系统
  4. Mybatis实现CRUD操作
  5. es文本分析java代码_Elasticsearch系列---Java客户端代码Demo
  6. Struts2的简单介绍
  7. windows7计算机用户账户,win7系统删除用户账户的方法(图文)
  8. Monkey测试:日志信息分析
  9. JAVA计算机毕业设计学生请假管理系统Mybatis+系统+数据库+调试部署
  10. 基于DS18B20温度控制系统
  11. 设备划分冲突域和广播域
  12. autocad r14 win7补丁_AutoCAD R14几个实用补丁
  13. maven项目中JRE System Library Problem J2SE-1.5问题
  14. html5中检测网络状态的方法,前端js监听浏览器网络变化
  15. 关于下载《Java程序员,上班那点事儿》的电子版
  16. 北大计算机考研822 911区别,北大考研成绩“上热搜”,4位神仙“打架”,旁人:倒吸一口气!...
  17. 0基础实现微信推送天气,生日等(女朋友快乐眼)
  18. 多项式 商环 域(群论笔记)
  19. lis25ba_实验LIS25BA骨振动传感器采集音频
  20. windows系统快捷调出任务管理器

热门文章

  1. python bytes转int_int与bytes的转换
  2. JAVA计算机毕业设计无人值守台球厅智能管理监控系统(附源码、数据库)
  3. redis批量删除key命令
  4. 台式计算机的配置清单表格,台式电脑配置清单60种.doc
  5. 3dmax透明贴图该透明的地方不透明
  6. Kubernetes K8S之kube-prometheus概述与部署
  7. 系统分析师学习笔记(二十)
  8. ubuntu 安装飞鸽传书
  9. 近百元受让老板娘股份 汉威电子员工持股计划亏损
  10. OpenCV-Python图像处理:插值方法及使用resize函数进行图像缩放