算是后缀数组的入门题吧。 思路无比简单,要是直接套模板的话应该很容易秒掉。

关于后缀数组看高中神犇的论文就可以学会了

算法合集之《后缀数组——处理字符串的有力工具》

话说这题暴力是可以过了,但是我们在做多校的时候就是用暴力过的,当时还不知道什么是后缀数组。。。

靠着概念纯手敲了几个小时,把建SA,求height,和RMQ的ST算法都复习了一遍,这个东西要是每次都手敲的话真的会死人,尤其是倍增算法基数排序怎么排怎么别扭。自己写的倍增算法又太长,大牛的倍增算法总感觉敲的不顺。

贴个代码做留念。。。

Front compression

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 1344    Accepted Submission(s): 500

Problem 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
 
Author
Zejun Wu (watashi)
 
Source
 
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define N 100100 char g[N];
int r[N];
int sa[N];
int scnt[N];
int wa[N],wb[N],wv[N];
int mrank[N];
int h[N],th[N];
int dp[N][22];
int save[N]; int cmp(int gg[],int a,int b,int k)
{
return gg[a]==gg[b] && gg[a+k]==gg[b+k];
} void getsa(int str[],int sa[],int n,int m)
{
int i,j,*x,*y,*t;
x=wa; y=wb;
memset(scnt,0,sizeof(scnt));
for(i=0;i<n;i++)
scnt[ x[i]=str[i] ]++;
for(i=1;i<m;i++)
scnt[i]+=scnt[i-1];
for(i=0;i<n;i++)
sa[ --scnt[ str[i] ] ]=i; for(int p=1,j=1;p<n;j*=2,m=p)
{
for(p=0,i=n-j;i<n;i++) y[p++]=i;
for(i=0;i<n;i++) if( sa[i]>=j ) y[p++]=sa[i]-j;
for(i=0;i<n;i++) wv[i]=x[ y[i] ];
memset(scnt,0,sizeof(scnt));
for(i=0;i<n;i++) scnt[ wv[i] ]++;
for(i=1;i<m;i++) scnt[i]+=scnt[i-1];
for(i=n-1;i>=0;i--) sa[ --scnt[ wv[i] ] ] = y[i];
for(p=1,t=x,x=y,y=t,x[sa[0]]=0,i=1;i<n;i++)
x[ sa[i] ] = cmp(y,sa[i],sa[i-1],j)?p-1:p++;
}
} void geth(int str[],int n)
{
h[n-1]=0;
int p=0;
for(int i=0;i<n-1;i++)
{
int tmp=mrank[i];
while( str[i+p] == str[ sa[tmp-1]+p ] ) p++;
h[i]=p;
p--;
p=max(0,p);
}
} void buildst(int n)
{
for(int i=1;i<=n;i++)
dp[i][0] = th[i];
for(int i=1;(1<<i)<=n;i++)
{
for(int j=1;j<=n;j++)
{
if( j+(1<<(i-1)) >n ) dp[j][i]=dp[j][i-1];
else dp[j][i]=min(dp[j][i-1],dp[j+(1<<(i-1))][i-1]);
}
}
} int cal(int x,int y)
{
if(x>y) swap(x,y);
x++;
//然后就是求x到y的最小值
int k=0;
while( (1<<k)<=(y-x+1) ) k++;
k--;
return min(dp[x][k],dp[y-(1<<k)+1][k]);
} int main()
{
while(scanf("%s",g)!=EOF)
{
int len=strlen(g);
for(int i=0;i<len;i++)
r[i]=g[i];
r[len++]=0;
getsa(r,sa,len,300);
for(int i=0;i<len;i++)
mrank[ sa[i] ]=i;
//for(int i=0;i<len;i++) printf("%s\n",g+sa[i]);
geth(r,len);
for(int i=0;i<len-1;i++)
th[ mrank[i] ]= h[i];
buildst(len-1);
int m;
long long ans1=0,ans2=0;
scanf("%d",&m);
int x,y,tmp;
scanf("%d%d",&x,&y);
ans1+=y-x; ans2+=y-x; save[1]=0; for(int i=2;i<=m;i++)
{
int tx,ty;
scanf("%d%d",&tx,&ty);
ans1+=ty-tx;
int cnt;
if(tx==x)
cnt=len-1-tx;
else
cnt=cal(mrank[tx],mrank[x]); save[i]=min(ty-tx,min(y-x,cnt));
ans2+=ty-tx-save[i];
x=tx; y=ty;
}
ans1+=m;
ans2+=m+m;
for(int i=1;i<=m;i++)
{
ans2++;
save[i]/=10;
while(save[i])
{
ans2++;
save[i]/=10;
}
}
cout<<ans1<<" "<<ans2<<endl;
}
return 0;
}

  

hdu4691(后缀数组)的更多相关文章

  1. hdu4691 Front compression ——暴力 || 后缀数组

    link:http://acm.hdu.edu.cn/showproblem.php?pid=4691 暴力,数据明显太水了吧,n=10^5, O(n^2)的复杂度哎喂.想让大家暴力写直接让n=100 ...

  2. HDU-4691 Front compression 后缀数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4691 后缀数组模板题,求出Height数组后,对Height做RMQ,然后直接统计就可以了... // ...

  3. hdu4691 Front compression(后缀数组)

    Front compression Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others) ...

  4. hdu 4691 最长的共同前缀 后缀数组 +lcp+rmq

    http://acm.hdu.edu.cn/showproblem.php? pid=4691 去年夏天,更多的学校的种族称号.当时,没有后缀数组 今天将是,事实上,自己的后缀阵列组合rmq或到,但是 ...

  5. 后缀数组的倍增算法(Prefix Doubling)

    后缀数组的倍增算法(Prefix Doubling) 文本内容除特殊注明外,均在知识共享署名-非商业性使用-相同方式共享 3.0协议下提供,附加条款亦可能应用. 最近在自学习BWT算法(Burrows ...

  6. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  7. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  8. POJ3693 Maximum repetition substring [后缀数组 ST表]

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9458   Acc ...

  9. POJ1743 Musical Theme [后缀数组]

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 De ...

随机推荐

  1. bootstrap学习笔记一 登录水平表单

    先上效果图: 样式定义: <form class="form-horizontal"> <div class="control-group"& ...

  2. win10 清理winsxs文件夹

    dism /online /cleanup-image /startcomponentcleanup /resetbase

  3. 在Linux下如何查CC攻击?

    什么是CC攻击?CC攻击就是利用大量代理服务器对目标计算机发起大量连接,导致目标服务器资源枯竭造成拒绝服务.那么如何判断查询CC攻击呢?本文主要介绍了一些Linux下判断CC攻击的命令. AD:201 ...

  4. jcifs 具体解释读取网络共享文件数据

    时隔1年半,没有发过新的帖子了,也没怎么来过CSDN逛逛了,人也懒散了. 今天收到网友的提问,才回来看看.认为应该再写点什么出来.只是.发现自己研究是不是太深入,写不出那么高深的东西.那就写点肤浅的东 ...

  5. 将HG版本库推送到Git服务器

    如何将HG版本库推送到Git服务器? 目的 习惯使用HG来进行版本管理,但是GitHub代码统计比Bitbucket要丰富,所以准备主力仓库选用Bitbucket,GitHub作为备用仓库. GitH ...

  6. Atitit.异常的设计原理与 策略处理 java 最佳实践 p93

    Atitit.异常的设计原理与 策略处理 java 最佳实践 p93 1 异常方面的使用准则,答案是:: 2 1.1 普通项目优先使用异常取代返回值,如果开发类库方面的项目,最好异常机制与返回值都提供 ...

  7. phpMyAdmin安装教程

    phpMyAdmin安装教程: 解压:将下载文件解压缩到 WEB 访问路径下.文件目录如phpmyadmin. 配置文件:然后配置目录下libraries文件下的 config.default.php ...

  8. Linux中如何设置服务自启动?

    转自:Linux中如何设置服务自启动? 有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务,主要用三种方式进行这一操作: ln -s             在/etc/rc.d/rc ...

  9. uboot中断功能实现

    uboot中实现irq中断(uboot version2015.04)1.实验目的:实现GPIO_2的外部中断 2.实验步骤:a.GPIO_2(GPIO1_IO02)为中断源, 首先需要设置这个pad ...

  10. select 自定义样式插件 selectize.js

    [特别推荐]几款极好的 JavaScript 下拉列表插件   表单元素让人爱恨交加.作为网页最重要的组成部分,表单几乎无处不在,从简单的邮件订阅.登陆注册到复杂的需要多页填写的信息提交功能,表单都让 ...