HDU 4691 后缀数组+RMQ
思路:
求一发后缀数组,求个LCP 就好了
注意数字有可能不只一位 (样例2)
//By SiriusRen
#include <bits/stdc++.h>
using namespace std;
const int N=;
char s[N];
int cntA[N],cntB[N],A[N],B[N],sa[N],rk[N],tsa[N],ht[N],f[N][];
int n,t,base[N],l[N],r[N];
void SA(){
for(int i=;i<=;i++)cntA[i]=;
for(int i=;i<=n;i++)cntA[s[i]]++;
for(int i=;i<=;i++)cntA[i]+=cntA[i-];
for(int i=n;i;i--)sa[cntA[s[i]]--]=i;
rk[sa[]]=;
for(int i=;i<=n;i++)rk[sa[i]]=rk[sa[i-]]+(s[sa[i]]!=s[sa[i-]]);
for(int l=;rk[sa[n]]<n;l<<=){
memset(cntA,,sizeof(cntA));
memset(cntB,,sizeof(cntB));
for(int i=;i<=n;i++)cntA[A[i]=rk[i]]++,cntB[B[i]=(i+l<=n?rk[i+l]:)]++;
for(int i=;i<=n;i++)cntA[i]+=cntA[i-],cntB[i]+=cntB[i-];
for(int i=n;i;i--)tsa[cntB[B[i]]--]=i;
for(int i=n;i;i--)sa[cntA[A[tsa[i]]]--]=tsa[i];
rk[sa[]]=;
for(int i=;i<=n;i++)rk[sa[i]]=rk[sa[i-]]+(A[sa[i]]!=A[sa[i-]]||B[sa[i]]!=B[sa[i-]]);
}
for(int i=,j=;i<=n;i++){
j=j?j-:;
while(s[i+j]==s[sa[rk[i]-]+j])j++;
ht[rk[i]]=j;
}
}
void init_rmq(){
base[]=-;
for(int i=;i<=n;i++)f[i][]=ht[i],base[i]=base[i>>]+;
for(int j=;j<=;j++)
for(int i=;i+(<<(j-))<=n;i++)
f[i][j]=min(f[i][j-],f[i+(<<(j-))][j-]);
}
int LCP(int x,int y){
if(x==y)return n-x+;
x=rk[x],y=rk[y];
if(x>y)swap(x,y);x++;
int t=base[y-x+];
return min(f[x][t],f[y-(<<t)+][t]);
}
int main(){
while(~scanf("%s",s+)){
long long ans=;n=strlen(s+);
SA(),init_rmq();
scanf("%d",&t);
for(int i=;i<=t;i++)scanf("%d%d",&l[i],&r[i]),ans+=r[i]-l[i]+,l[i]++;
printf("%lld ",ans);
for(int i=;i<=t;i++){
int tmp=min(LCP(l[i-],l[i]),min(r[i]-l[i],r[i-]-l[i-])+);
ans+=(tmp?(int)log10(tmp):)-tmp;
}
printf("%lld\n",ans+t*);
}
}
HDU 4691 后缀数组+RMQ的更多相关文章
- hdu 2459 (后缀数组+RMQ)
题意:让你求一个串中连续重复次数最多的串(不重叠),如果重复的次数一样多的话就输出字典序小的那一串. 分析:有一道比这个简单一些的题spoj 687, 假设一个长度为l的子串重复出现两次,那么它必然会 ...
- hdu 3948(后缀数组+RMQ)
题意:求一个串中有多少不同的回文串. 分析:这一题的关键是如何去重,我表示我现在还没理解为什么这样去重,先放这里过两天再看!! //不同回文子串数目 #include <iostream> ...
- hdu 3948 后缀数组
The Number of Palindromes Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (J ...
- 【uva10829-求形如UVU的串的个数】后缀数组+rmq or 直接for水过
题意:UVU形式的串的个数,V的长度规定,U要一样,位置不同即为不同字串 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&am ...
- POJ 3693 后缀数组+RMQ
思路: 论文题 后缀数组&RMQ 有一些题解写得很繁 //By SiriusRen #include <cmath> #include <cstdio> #includ ...
- spoj687 REPEATS - Repeats (后缀数组+rmq)
A string s is called an (k,l)-repeat if s is obtained by concatenating k>=1 times some seed strin ...
- HDU 6194 string string string(后缀数组+RMQ)
string string string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU - 3948 后缀数组+Manacher
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3948 题意:给定一个字符串,求字符串本质不同的回文子串个数. 思路:主要参考该篇解题报告 先按照man ...
- HDU2459 后缀数组+RMQ
题目大意: 在原串中找到一个拥有连续相同子串最多的那个子串 比如dababababc中的abababab有4个连续的ab,是最多的 如果有同样多的输出字典序最小的那个 这里用后缀数组解决问题: 枚举连 ...
随机推荐
- 【CodeChef】KNGHTMOV(方案数DP)
题意: 考虑一张无限大的方格棋盘.我们有一个“骑士”,它必须从(0,0)格开始,按照如下规则,移动至(X,Y)格:每一步,它只能从(u,v)格移动至(u+Ax,v+Ay)或者(u+Bx,v+By).注 ...
- Qmake 工具编译调试
Qmake 工具编译调试 2015年4月9日星期四 18:38:06 1. 确定qmaek 路径 [root@roger ~]# which qmake /usr/lib/qt-3.3/bin/qma ...
- Bad Luck Island-CodeForce(dp)
链接:http://codeforces.com/problemset/problem/540/D 题目大意: 这个岛上有三种生物 r石头 s剪刀 p布 求最后只剩一种生物的概率 用dp[i][ ...
- 恢复表数据的办法(delete删除可恢复,truncate不可恢复)
select * from table_name as of timestamp to_timestamp('2018-12-20 00:00:00', 'yyyy-mm-dd hh24:mi:ss' ...
- SystemTap 学习笔记 - 安装篇
https://segmentfault.com/a/1190000000671438 在安装前,需要知道下自己的系统环境,我的环境如下: uname -r 2.6.18-308.el5 Linux ...
- kafka-manager 的编译和使用(附安装包)
kafka-manager 的编译和使用(附安装包) 学习了:https://my.oschina.net/wangjiankui/blog/653139
- UML中的四种关系总结
UML中的关系主要包含四种:关联关系.依赖关系.泛化关系.实现关系.当中关联关系还包含聚合关系和组合关系. 1. 关联关系(Association) 关联关系式一种结构化的关系,是指一种对象和还有一种 ...
- linux一些硬件详情查看的高级方法(网卡,内存,硬盘,cpu)
网卡-lspci内存大小和个数—— dmidecode|grep -A16 "Memory Device$"查看硬盘型号——smartctl -a /dev/sda查看硬盘大小—— ...
- 1062. Talent and Virtue (25)【排序】——PAT (Advanced Level) Practise
题目信息 1062. Talent and Virtue (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B About 900 years ago, a Chine ...
- C Language Study - gets , getchar & scanf
慢慢的发现C语言功底是如此的薄弱,被这几个字符输入函数搞糊涂了又~~ 来,再来忧伤一次吧~ 那么.我们从scanf開始: 假如说你要将一串字符输入到一字符数组里,例如以下面程序, char a[2]; ...