传送门

很容易想到,题目中的相同是指差分数组相同。

那么可以把差分数组连起来,中间加上一个没有出现过的且字典序小的数

双指针移动,用st表维护height数组中的最小值。

当然用单调队列应该也可以且更快。

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 1010000 using namespace std; int n, m, t, cnt, len, ans, mx, mn = 1e9;
int pos[N], a[N / 1000], num[N / 1000], sa[N], height[N], Rank[N], b[N], x[N], y[N], d[N][22], s[N]; inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
} inline void build_sa()
{
int i, k, p;
for(i = 0; i < m; i++) b[i] = 0;
for(i = 0; i < n; i++) b[x[i] = s[i]]++;
for(i = 1; i < m; i++) b[i] += b[i - 1];
for(i = n - 1; i >= 0; i--) sa[--b[x[i]]] = i;
for(k = 1; k <= n; k <<= 1)
{
p = 0;
for(i = n - k; i < n; i++) y[p++] = i;
for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;
for(i = 0; i < m; i++) b[i] = 0;
for(i = 0; i < n; i++) b[x[y[i]]]++;
for(i = 1; i < m; i++) b[i] += b[i - 1];
for(i = n - 1; i >= 0; i--) sa[--b[x[y[i]]]] = y[i];
swap(x, y);
p = 1, x[sa[0]] = 0;
for(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;
}
} inline void build_height()
{
int i, j, k = 0;
for(i = 0; i < n; i++) Rank[sa[i]] = i;
for(i = 0; i < n; i++)
{
if(k) k--;
j = sa[Rank[i] - 1];
while(s[i + k] == s[j + k]) k++;
height[Rank[i]] = k;
}
} inline void build_st()
{
int i, j;
for(i = 1; i < n; i++) d[i][0] = height[i];
for(j = 1; (1 << j) < n; j++)
for(i = 1; i + (1 << j) - 1 < n; i++)
d[i][j] = min(d[i][j - 1], d[i + (1 << j - 1)][j - 1]);
} inline int query(int l, int r)
{
l++;
if(l > r) return 0;
int tmp = log2(r - l + 1);
return min(d[l][tmp], d[r - (1 << tmp) + 1][tmp]);
} inline void solve()
{
int i, j = 0;
for(i = 0; i < n; i++)
{
while(j < n && cnt < t)
{
if(!num[pos[sa[j]]]++ && pos[sa[j]]) cnt++;
j++;
}
if(cnt == t) ans = max(ans, query(i, j - 1));
if(!--num[pos[sa[i]]] && pos[sa[i]]) cnt--;
}
} int main()
{
int i, j, k;
t = read();
for(i = 1; i <= t; i++)
{
k = read();
for(j = 1; j <= k; j++) a[j] = read();
for(j = 1; j < k; j++)
{
pos[n] = i;
s[n++] = a[j + 1] - a[j];
mn = min(mn, s[n - 1]);
}
s[n++] = mx++;
}
for(i = 0; i < n; i++)
if(pos[i]) s[i] = s[i] - mn + mx, m = max(m, s[i]);
m++;
build_sa();
build_height();
build_st();
solve();
printf("%d\n", ans + 1);
return 0;
}

  

[luoguP2463] [SDOI2008]Sandy的卡片(后缀数组 + st表)的更多相关文章

  1. 【BZOJ4698】Sdoi2008 Sandy的卡片 后缀数组+RMQ

    [BZOJ4698]Sdoi2008 Sandy的卡片 Description Sandy和Sue的热衷于收集干脆面中的卡片.然而,Sue收集卡片是因为卡片上漂亮的人物形象,而Sandy则是为了积攒卡 ...

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

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

  3. BZOJ 4698: Sdoi2008 Sandy的卡片 后缀数组 + RMQ + 查分

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

  4. SDOI2008 Sandy的卡片( 后缀数组 )

    求出后缀数组, 然后二分答案, 对height数组分组检验答案. 时间复杂度O(|S| log|S|) ------------------------------------------------ ...

  5. BZOJ4698: Sdoi2008 Sandy的卡片(后缀数组 二分)

    题意 题目链接 Sol 不要问我为什么发两篇blog,就是为了骗访问量 后缀数组的也比较好想,先把所有位置差分,然后在height数组中二分就行了 数据好水啊 // luogu-judger-enab ...

  6. BZOJ 4698: Sdoi2008 Sandy的卡片(后缀数组+差分+二分答案)

    传送门 解题思路 看到一个子串加一个数字到另一个子串,自然可以想到差分.然后要把所有串都拼起来,求出\(height\)数组后可以二分答案来做,每次二分一个答案后统计一下连续的\(height> ...

  7. 洛谷P2463 [SDOI2008]Sandy的卡片(后缀数组SA + 差分 + 二分答案)

    题目链接:https://www.luogu.org/problem/P2463 [题意] 求出N个串中都出现的相同子串的最长长度,相同子串的定义如题:所有元素加上一个数变成另一个,则这两个串相同,可 ...

  8. SPOJ 687 Repeats(后缀数组+ST表)

    [题目链接] http://www.spoj.com/problems/REPEATS/en/ [题目大意] 求重复次数最多的连续重复子串的长度. [题解] 考虑错位匹配,设重复部分长度为l,记s[i ...

  9. POJ 3693 Maximum repetition substring(后缀数组+ST表)

    [题目链接] poj.org/problem?id=3693 [题目大意] 求一个串重复次数最多的连续重复子串并输出,要求字典序最小. [题解] 考虑错位匹配,设重复部分长度为l,记s[i]和s[i+ ...

  10. BZOJ_4516_[Sdoi2016]生成魔咒_后缀数组+ST表+splay

    BZOJ_4516_[Sdoi2016]生成魔咒_后缀数组+ST表+splay Description 魔咒串由许多魔咒字符组成,魔咒字符可以用数字表示.例如可以将魔咒字符 1.2 拼凑起来形成一个魔 ...

随机推荐

  1. codeforce Gym 100500E IBM Chill Zone (SG函数)

    关于sg函数这篇blog讲得很详细http://blog.csdn.net/logic_nut/article/details/4711489. sg函数的价值在于把复杂的游戏拆分成简单的游戏,然后通 ...

  2. Set、Map及数组去重

    https://cloud.tencent.com/developer/article/1437254 https://blog.csdn.net/weixin_34247299/article/de ...

  3. jquerymobi总结

    http://app-framework-software.intel.com/ http://app-framework-software.intel.com/api2/#$_proxy

  4. java中求几个字符串的最大公共子串 使用了比较器Comparator

    package com.swift; import java.util.ArrayList; import java.util.Collections; import java.util.Compar ...

  5. NOIP模拟赛 czy的后宫

    [题目描述] czy要妥善安排他的后宫,他想在机房摆一群妹子,一共有n个位置排成一排,每个位置可以摆妹子也可以不摆妹子.有些类型妹子如果摆在相邻的位置(隔着一个空的位置不算相邻),就不好看了.假定每种 ...

  6. 【思维题 状压dp】APC001F - XOR Tree

    可能算是道中规中矩的套路题吧…… Time limit : 2sec / Memory limit : 256MB Problem Statement You are given a tree wit ...

  7. 图解一致性协议2PC和3PC

    原图地址:https://www.processon.com/diagraming/5b89f6ace4b0d4d65bf10786

  8. Vue表单输入绑定

    <h3>基础用法</h3> <p>你可以用<strong>v-model</strong>指令在表单input,textarea以及sele ...

  9. Python函数的基本定义和调用以及内置函数

    首先我们要了解Python函数的基本定义: 函数是什么? 函数是可以实现一些特定功能的小方法或是小程序.在Python中有很多内建函数,当然随着学习的深入,你也可以学会创建对自己有用的函数.简单的理解 ...

  10. R-codes-tips

    1. 在shell执行R文件 chmod 0755 file.R Rscript file.R 2.  载入数据 data(dune) 3. attach() 将data.frame添加到R的搜索路径 ...