loj2059 「TJOI / HEOI2016」字符串
#include <iostream>
#include <cstdio>
using namespace std;
int n, m, rnk[100005], cnt[100005], tmp[100005], p, mx=128, saa[100005], tot, a, b;
int height[100005], gg[19][100005], mlg[100005], rot[100005], c, d;
char ss[100005];
struct Node{
int l, r, sum;
}nd[2000005];
void sasort(){
for(int i=0; i<mx; i++) cnt[i] = 0;
for(int i=0; i<n; i++) cnt[rnk[i]]++;
for(int i=1; i<mx; i++) cnt[i] += cnt[i-1];
for(int i=n-1; i>=0; i--) saa[--cnt[rnk[tmp[i]]]] = tmp[i];
}
void getSa(){
for(int i=0; i<n; i++){
rnk[i] = ss[i];
tmp[i] = i;
}
sasort();
for(int j=1; p<n; j<<=1){
p = 0;
for(int i=n-j; i<n; i++) tmp[p++] = i;
for(int i=0; i<n; i++)
if(saa[i]>=j)
tmp[p++] = saa[i] - j;
sasort();
swap(rnk, tmp);
p = 1;
rnk[saa[0]] = 0;
for(int i=1; i<n; i++)
if(tmp[saa[i-1]]==tmp[saa[i]] && tmp[saa[i-1]+j]==tmp[saa[i]+j])
rnk[saa[i]] = p - 1;
else
rnk[saa[i]] = p++;
mx = p;
}
}
void getHeight(){
int h=0;
for(int i=0; i<n; i++){
if(h) h--;
int j=saa[rnk[i]-1];
while(ss[i+h]==ss[j+h]) h++;
height[rnk[i]] = h;
}
}
void getRmq(){
for(int i=1; i<=n; i++) gg[0][i] = height[i];
for(int j=1; j<=17; j++)
for(int i=1; i<=n; i++){
if(i+(1<<(j-1))>n) break;
gg[j][i] = min(gg[j-1][i], gg[j-1][i+(1<<(j-1))]);
}
}
int update(int pre, int l, int r, int x){
int re=++tot;
nd[re] = nd[pre];
nd[re].sum++;
if(l==r) ;
else{
int mid=(l+r)>>1;
if(x<=mid) nd[re].l = update(nd[pre].l, l, mid, x);
else nd[re].r = update(nd[pre].r, mid+1, r, x);
}
return re;
}
bool query(int pre, int now, int l, int r, int x, int y){
if(l>=x && r<=y) return nd[now].sum-nd[pre].sum>0;
else{
int mid=(l+r)>>1;
bool re=false;
if(x<=mid) re |= query(nd[pre].l, nd[now].l, l, mid, x, y);
if(mid<y) re |= query(nd[pre].r, nd[now].r, mid+1, r, x, y);
return re;
}
}
int main(){
cin>>n>>m;
scanf("%s", ss);
ss[n++] = 0;
getSa();
n--;
getHeight();
getRmq();
for(int i=1; i<=n; i++)
rot[i] = update(rot[i-1], 1, n, rnk[i-1]);
while(m--){
scanf("%d %d %d %d", &a, &b, &c, &d);
int l=1, r=min(b-a+1, d-c+1), mid, re=0;
while(l<=r){
mid = (l + r) >> 1;
int zuo=rnk[c-1], you=rnk[c-1];
for(int i=17; i>=0; i--)
if(zuo>=(1<<i) && gg[i][zuo-(1<<i)+1]>=mid)
zuo -= 1<<i;
for(int i=17; i>=0; i--)
if(you+(1<<i)<=n && gg[i][you+1]>=mid)
you += 1<<i;
if(query(rot[a-1], rot[b-mid+1], 1, n, zuo, you))
re = mid, l = mid + 1;
else
r = mid - 1;
}
printf("%d\n", re);
}
return 0;
}
loj2059 「TJOI / HEOI2016」字符串的更多相关文章
- 「TJOI / HEOI2016」字符串
「TJOI / HEOI2016」字符串 题目描述 佳媛姐姐过生日的时候,她的小伙伴从某东上买了一个生日礼物.生日礼物放在一个神奇的箱子中.箱子外边写了一个长为 \(n\) 的字符串 \(s\),和 ...
- loj#2059. 「TJOI / HEOI2016」字符串 sam+线段树合并+倍增
题意:给你一个子串,m次询问,每次给你abcd,问你子串sa-b的所有子串和子串sc-d的最长公共前缀是多长 题解:首先要求两个子串的最长公共前缀就是把反过来插入变成最长公共后缀,两个节点在paren ...
- 【LOJ】#2059. 「TJOI / HEOI2016」字符串
题解 我们冷静一下,先画一棵后缀树 然后发现我们要给c和d这一段区间在[a,b]这一段开头的串里找lcp 而lcp呢,就是c点的祖先的到根的一段,假如这个祖先的子树里有[a,b - dis[u] + ...
- loj#2054. 「TJOI / HEOI2016」树
题目链接 loj#2054. 「TJOI / HEOI2016」树 题解 每次标记覆盖整棵字数,子树维护对于标记深度取max dfs序+线段树维护一下 代码 #include<cstdio> ...
- AC日记——#2054. 「TJOI / HEOI2016」树
#2054. 「TJOI / HEOI2016」树 思路: 线段树: 代码: #include <cstdio> #include <cstring> #include < ...
- AC日记——#2057. 「TJOI / HEOI2016」游戏 LOJ
#2057. 「TJOI / HEOI2016」游戏 思路: 最大流: 代码: #include <cstdio> #include <cstring> #include &l ...
- loj #2055. 「TJOI / HEOI2016」排序
#2055. 「TJOI / HEOI2016」排序 题目描述 在 2016 年,佳媛姐姐喜欢上了数字序列.因而他经常研究关于序列的一些奇奇怪怪的问题,现在他在研究一个难题,需要你来帮助他. 这个 ...
- loj2058 「TJOI / HEOI2016」求和 NTT
loj2058 「TJOI / HEOI2016」求和 NTT 链接 loj 思路 \[S(i,j)=\frac{1}{j!}\sum\limits_{k=0}^{j}(-1)^{k}C_{j}^{k ...
- LOJ #2058「TJOI / HEOI2016」求和
不错的推柿子题 LOJ #2058 题意:求$\sum\limits_{i=0}^n\sum\limits_{j=0}^nS(i,j)·2^j·j!$其中$ S(n,m)$是第二类斯特林数 $ Sol ...
随机推荐
- form表单上传域(type="file")的使用----上传文件
一,单个文件的上传 1.html/jsp页面 <%@ page language="java" contentType="text/html; charset=UT ...
- Masonry 等间隔或等宽高排列多个控件
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...
- android-上下文菜单的创建 - 随心
//Menu设置//覆盖两个方法onCreateOptionsMenu(Menu menu).onOptionsItemSelected(MenuItem Item)//onCreateOptions ...
- mysql服务器系统优化
1.选择合适的IO调度 对于mysql的系统,如果是SSD,那么应该使用NOOP调度算法,如果是磁盘,就应该使用Deadline调度算法.默认是CFQ echo dealine > /sys/b ...
- linux 命令——18 locate (转)
locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案.其方法是先建立一个包括系统内所有档案名称及路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中了.在一般的 di ...
- IOS Window窗口使用
// 程序启动完毕之后就会调用一次 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NS ...
- 如何更改VirtualBox虚拟电脑内存大小
- Drupal的入门学习
1. 注意content中的区别 Article和Basic page的区别 a.输入字段不一样,Article内容多了两个字段:tag和图片. b.内容的默认设置不一样,Article默认允许评论, ...
- SpringBoot学习记录(一)
1. Spring的Java配置方式 Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.1. @Configuration 和 @Bean Spring的Java配置方式是 ...
- 扩展 -------jQuery
本文摘要:http://www.liaoxuefeng.com/ 编写jQuery插件 为了满足需求,我们经常会调用一些插件,js插件都是别人写的,今天就来了解了解一些方法. 给jQuery对象绑定一 ...