HDU 4691 Front compression(后缀数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4691
题意:给出Input,求出Compressed output。输出各用多少字节。
思路:求后缀数组。每次直接询问两个后缀的最长公共前缀。
int r[N],sa[N],wa[N],wb[N],wd[N],rank[N],h[N];
int cmp(int *r,int a,int b,int len)
{
return r[a]==r[b]&&r[a+len]==r[b+len];
}
void da(int *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
FOR0(i,m) wd[i]=0;
FOR0(i,n) wd[x[i]=r[i]]++;
FOR1(i,m-1) wd[i]+=wd[i-1];
FORL0(i,n-1) sa[--wd[x[i]]]=i;
for(j=1,p=1;p<n;j<<=1,m=p)
{
p=0;
FOR(i,n-j,n-1) y[p++]=i;
FOR0(i,n) if(sa[i]>=j) y[p++]=sa[i]-j;
FOR0(i,m) wd[i]=0;
FOR0(i,n) wd[x[i]]++;
FOR1(i,m-1) wd[i]+=wd[i-1];
FORL0(i,n-1) sa[--wd[x[y[i]]]]=y[i];
t=x;x=y;y=t;p=1;x[sa[0]]=0;
FOR1(i,n-1) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
}
void calHeight(int *r,int *sa,int n)
{
int i,j,k=0;
FOR1(i,n) rank[sa[i]]=i;
FOR0(i,n)
{
if(k) k--;
j=sa[rank[i]-1];
while(i+k<n&&j+k<n&&r[i+k]==r[j+k]) k++;
h[rank[i]]=k;
}
}
int f[N][20],n;
void init()
{
int i,j;
FOR1(i,n) f[i][0]=h[i];
for(i=1;1+(1<<i)<=n;i++) for(j=1;j+(1<<i)-1<=n;j++)
{
f[j][i]=min(f[j][i-1],f[j+(1<<(i-1))][i-1]);
}
}
int cal(int a,int b)
{
if(a==b) return n-a;
a=rank[a];
b=rank[b];
if(a>b) swap(a,b);
a++;
int m=floor(log(1.0*(b-a+1))/log(2.0));
return min(f[a][m],f[b-(1<<m)+1][m]);
}
char s[N];
int getBit(int x)
{
if(x<10) return 1;
int ans=0;
while(x) ans++,x/=10;
return ans;
}
void deal()
{
i64 ans1=0,ans2=0;
int preL,preR,L,R;
int m,k;
RD(m);
RD(preL,preR);
ans1=preR-preL+1;
ans2=preR-preL+3;
m--;
while(m--)
{
RD(L,R);
k=min(min(R-L,preR-preL),cal(L,preL));
ans1+=R-L+1;
ans2+=getBit(k)+1+R-L-k+1;
preL=L;
preR=R;
}
printf("%I64d %I64d\n",ans1,ans2);
}
int main()
{
while(scanf("%s",s)!=-1)
{
n=strlen(s);
int i;
FOR0(i,n) r[i]=s[i];
r[n]=0;
da(r,sa,n+1,130);
calHeight(r,sa,n);
init();
deal();
}
}
HDU 4691 Front compression(后缀数组)的更多相关文章
- hdu 4691 Front compression (后缀数组)
hdu 4691 Front compression 题意:很简单的,就是给一个字符串,然后给出n个区间,输出两个ans,一个是所有区间的长度和,另一个是区间i跟区间i-1的最长公共前缀的长度的数值的 ...
- HDU-4691 Front compression 后缀数组
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4691 后缀数组模板题,求出Height数组后,对Height做RMQ,然后直接统计就可以了... // ...
- HDU 4691 Front compression (2013多校9 1006题 后缀数组)
Front compression Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Othe ...
- hdu4691 Front compression(后缀数组)
Front compression Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others) ...
- HDU 4691 正解后缀数组(暴力也能过)
本来是个后缀数组,考察算法的中级题目,暴力居然也可以水过,就看你跳不跳坑了(c++和G++返回结果就很不一样,关键看编译器) 丝毫不差的代码,就看运气如何了.唯一差别c++还是G++,但正解是后缀数组 ...
- hdu 4691 Front compression
暴力水过,剪一下枝= =果断是数据水了 #include<cstdio> #include<cstring> #include<algorithm> #define ...
- 【HDOJ】4691 Front compression
后缀数组基础题目,dc3解. /* 4691 */ #include <iostream> #include <sstream> #include <string> ...
- hdu 5442 Favorite Donut 后缀数组
Favorite Donut Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid ...
- hdu 4622 Reincarnation(后缀数组)
hdu 4622 Reincarnation 题意:还是比较容易理解,给出一个字符串,最长2000,q个询问,每次询问[l,r]区间内有多少个不同的字串. (为了与论文解释统一,这里解题思路里sa数组 ...
随机推荐
- 【BZOJ】【4002】【JLOI2015】有意义的字符串
构造线性递推式+矩阵乘法 题解戳PoPoQQQ 为了自己以后看的方便手打一遍好了>_> 求$( \frac{b+\sqrt{d}}{2} )^n$的整数部分对p取模后的值 其中$b\mod ...
- 安装成功的nginx如何添加未编译安装模块
原已经安装好的nginx,现在需要添加一个未被编译安装的模块举例说明:安装第三方的ngx_cache_purge模块(用于清除指定URL的缓存)nginx的模块是需要重新编译nginx,而不是像apa ...
- 寒假222_Topcoder SRM648
序号1: A,随便模拟好了,最后30秒发现一个都比的错误,无奈输错格式. 2: B,问你出去两个点,以及所产生的边 问你:产生多的联通快 加答案加1. 数据小,随便写暴力 3: copy思路. 我们先 ...
- PHP soap Web Service 使用SoapDiscovery.class.php 生成wsdl文件
PHP soap web service 使用wsdl文件 demo: ============================================================== 服 ...
- hadoop-ha QJM 架构部署
公司之前老的hadoop集群namenode有单点风险,最近学习此链接http://www.binospace.com/index.php /hdfs-ha-quorum-journal-manage ...
- angularjs取Sevice和directive的引用
取Sevice和directive的引用 3: Grab any Services We can grab a reference to any service using the injector ...
- Codis集群的搭建与使用
一.简介 Codis是一个分布式的Redis解决方案,对于上层的应用来说,连接Codis Proxy和连接原生的Redis Server没有明显的区别(不支持的命令列表),上层应用可以像使用单机的Re ...
- webservice wsdl 生成服务
由于之前的示例是在当前项目下发布的server,也是在当前项目下访问的server发布的webservice.但在实际应用中,我们的服务端往往是和客户諯分离的,甚至它们是不同的项目中不同的人写的.而像 ...
- linux权威指南 简记
/proc 目录,linxu系统以文件形式存放进程信息,这是一个虚拟的文件系统,不占有任何磁盘空间,当读取该文件系统时,系统内核会拦截动作,并动态产生文件与目录的内容 查看该文件夹,会发现很多已数字命 ...
- 翻译 - NodeJS错误处理最佳实践
王龑 - APRIL 13, 2015 NodeJS的错误处理让人痛苦,在很长的一段时间里,大量的错误被放任不管.但是要想建立一个健壮的Node.js程序就必须正确的处理这些错误,而且这并不难学.如果 ...