HDU-6704 K-th occurrence
Description
You are given a string S consisting of only lowercase english letters and some queries.
For each query (l,r,k), please output the starting position of the k-th occurence of the substring $S_lS_{l+1}...S_r $in S.
Input
The first line contains an integer T(1≤T≤20), denoting the number of test cases.
The first line of each test case contains two integer N(1≤N≤\(10^5\)),Q(1≤Q≤\(10^5\)), denoting the length of S and the number of queries.
The second line of each test case contains a string S(|S|=N) consisting of only lowercase english letters.
Then Q lines follow, each line contains three integer l,r(1≤l≤r≤N) and k(1≤k≤N), denoting a query.
There are at most 5 testcases which N is greater than \(10^3\).
Output
For each query, output the starting position of the k-th occurence of the given substring.
If such position don't exists, output −1 instead.
Sample Input
2
12 6
aaabaabaaaab
3 3 4
2 3 2
7 8 3
3 4 2
1 4 2
8 12 1
1 1
a
1 1 1
Sample Output
5
2
-1
6
9
8
1
题解
给定一个字符串,每次询问[l,r]的字符串第k次出现的位置,没有则输出-1
后缀数组理解深刻的话应该可以秒掉这道题
首先,height[i]表示排名第i位的和第i-1位的最长公共前缀,所以我们要找某个子串出现的所有位置,只需要在height数组中二分,询问的字串所处后缀的排名即为\(rk[l]\),那么我们从\(rk[l]\)开始向上向下二分,让这段的区间height最小值大于r-l+1,那么他们就都有r-l+1的最长公共前缀,我们找出这个边界后,用主席树求这个区间中sa数组的第k大即可。
AC代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
typedef long long ll;
char s[N];
int x[N], y[N], c[N], sa[N], rk[N], height[N];
int n, m, q;
void tsort() {
for (int i = 0; i <= m; i++) c[i] = 0;
for (int i = 1; i <= n; i++) c[x[i]]++;
for (int i = 2; i <= m; i++) c[i] += c[i - 1];
for (int i = n; i >= 1; i--) sa[c[x[y[i]]]--] = y[i];
}
void get_sa() {
memset(c, 0, sizeof(c));
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
for (int i = 1; i <= n; i++) x[i] = s[i], y[i] = i;
tsort();
for (int k = 1; k <= n; k <<= 1) {
int num = 0;
for (int i = n - k + 1; i <= n; i++) y[++num] = i;
for (int i = 1; i <= n; i++) if (sa[i] > k) y[++num] = sa[i] - k;
tsort();
swap(x, y);
x[sa[1]] = 1;
num = 1;
for (int i = 2; i <= n; i++)
x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) ? num : ++num;
if (num == n) break;
m = num;
}
}
void get_h() {
int k = 0;
for (int i = 1; i <= n; i++) rk[sa[i]] = i;
for (int i = 1; i <= n; i++) {
if (rk[i] == 1) continue;
if (k) k--;
int j = sa[rk[i] - 1];
while (s[i + k] == s[j + k]) k++;
height[rk[i]] = k;
}
}
int st[N][20], lg2[N];
void ST() {
for (int i = 1; i <= n; i++) {
st[i][0] = height[i];
}
for (int j = 1; (1 << j) <= n; j++) {
for (int i = 1; (i + (1 << j) - 1) <= n; i++) {
st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
for (int i = 2; i <= n; i++) {
lg2[i] = lg2[i >> 1] + 1;
}
}
int rmq(int l, int r) {
if (l > r)
return 0;
else {
int x = lg2[r - l + 1];
return min(st[l][x], st[r - (1 << x) + 1][x]);
}
}
int L[N * 40], R[N * 40], T[N], cnt;
ll sum[N * 40];
int build(int l, int r) {
int rt = ++cnt;
sum[rt] = 0;
int mid = (l + r) >> 1;
if (l < r) {
L[rt] = build(l, mid);
R[rt] = build(mid + 1, r);
}
return rt;
}
int update(int pre, int l, int r, int x) {
int rt = ++cnt;
int mid = (l + r) >> 1;
L[rt] = L[pre], R[rt] = R[pre], sum[rt] = sum[pre] + 1;
if (l < r) {
if (x <= mid) L[rt] = update(L[pre], l, mid, x);
else R[rt] = update(R[pre], mid + 1, r, x);
}
return rt;
}
int query(int u, int v, int l, int r, int k) {
if (l >= r) {
return l;
}
int mid = (l + r) >> 1;
int x = sum[L[v]] - sum[L[u]];
if (x >= k) return query(L[u], L[v], l, mid, k);
else {
if (sum[R[v]] - sum[R[u]] < k - x) return -1;
return query(R[u], R[v], mid + 1, r, k - x);
}
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
m = 130;
scanf("%d%d", &n, &q);
scanf("%s", s + 1);
cnt = 0;
get_sa();
get_h();
ST();
T[0] = build(1, n);
for (int i = 1; i <= n; i++) {
T[i] = update(T[i - 1], 1, n, sa[i]);
}
while (q--) {
int l, r, k;
scanf("%d%d%d", &l, &r, &k);
int tl = rk[l], tr = rk[l];
int x = 1, y = rk[l];
while (x <= y) {
int mid = (x + y) >> 1;
if (rmq(mid, rk[l]) >= r - l + 1) {
y = mid - 1;
tl = min(tl, mid - 1);//注意细节
}
else x = mid + 1;
}
x = rk[l] + 1, y = n;//注意细节
while (x <= y) {
int mid = (x + y) >> 1;
if (rmq(rk[l] + 1, mid) >= r - l + 1) {
x = mid + 1;
tr = max(tr, mid);
}
else y = mid - 1;
}
printf("%d\n", query(T[tl - 1], T[tr], 1, n, k));
}
}
return 0;
}
HDU-6704 K-th occurrence的更多相关文章
- 2019CCPC网络赛 C - K-th occurrence HDU - 6704(后缀数组+ST表+二分+主席树)
题意 求区间l,r的子串在原串中第k次出现的位置. 链接:https://vjudge.net/contest/322094#problem/C 思路 比赛的时候用后缀自动机写的,TLE到比赛结束. ...
- K-th occurrence HDU - 6704 (SA, 主席树)
大意: 给定串$s$, $q$个询问$(l,r,k)$, 求子串$s[l,r]$的第$k$次出现位置. 本来是个简单签到题, 可惜比赛的时候还没学$SA$...... 好亏啊 相同的子串在$SA$中是 ...
- HDU - 6704 K-th occurrence (后缀数组+主席树/后缀自动机+线段树合并+倍增)
题意:给你一个长度为n的字符串和m组询问,每组询问给出l,r,k,求s[l,r]的第k次出现的左端点. 解法一: 求出后缀数组,按照排名建主席树,对于每组询问二分或倍增找出主席树上所对应的的左右端点, ...
- K-th occurrence HDU - 6704 (后缀数组+二分线段树+主席树)
大意: 给定串s, q个询问(l,r,k), 求子串s[l,r]的第kk次出现位置. 这是一篇很好的题解: https://blog.csdn.net/sdauguanweihong/article/ ...
- hdu 6704 K-th occurrence(后缀数组+可持久化线段树)
Problem Description You are given a string S consisting of only lowercase english letters and some q ...
- HDU 6704 K-th occurrence(主席树 + RMQ + 后缀数组)题解
题意: 给一个串\(S\),\(length\leq 1e5\),\(Q\leq1e5\)个询问,每次询问输出和\(S_lS_{l+1}\dots S_r\)长得一模一样的第\(k\)个子串的开头位置 ...
- HDU 5122 K.Bro Sorting(模拟——思维题详解)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5122 Problem Description Matt's friend K.Bro is an A ...
- HDU 5122 K.Bro Sorting(2014北京区域赛现场赛K题 模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5122 解题报告:定义一种排序算法,每一轮可以随机找一个数,把这个数与后面的比这个数小的交换,一直往后判 ...
- HDU 5122 K.Bro Sorting
K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Tot ...
- 基础题:HDU 5122 K.Bro Sorting
Matt's friend K.Bro is an ACMer.Yesterday, K.Bro learnt an algorithm: Bubble sort. Bubble sort will ...
随机推荐
- [19/06/05-星期三] CSS基础_样式表的位置(内联、内部、外部样式)、块元素(div)、内联元素(span)、常用的选择器
一.概念 CSS(Cascading Style Sheets,层叠样式表) 可以用来为网页创建样式表,通过样式表可以对网页进行装饰. 所谓层叠,就是可以将整个网页想象成是一层一层的结构,层次高的将会 ...
- Sql server 执行计划详解
序言 本篇主要目的有二: 1.看懂t-sql的执行计划,明白执行计划中的一些常识. 2.能够分析执行计划,找到优化sql性能的思路或方案. 如果你对sql查询优化的理解或常识不是很深入,那么推荐几骗博 ...
- 如何在ubuntu上安装 搜狗输入法(已经成功)
转自:https://blog.csdn.net/qq_37589838/article/details/81208409 本文链接:https://blog.csdn.net/qq_37589838 ...
- 【JZOJ 3909】Idiot 的乘幂
题面: 正文: 把题目中的方程组组合在一起就变成了: \(X^{a+c}\equiv b \cdot d (\mod p)\) 那这时,我们假定两个数\(x\)和\(y\),使得: \(ax + cy ...
- react 前端项目技术选型、开发工具、周边生态
react 前端项目技术选型.开发工具.周边生态 声明:这不是一篇介绍 React 基础知识的文章,需要熟悉 React 相关知识 主架构:react, react-router, redux, re ...
- 【转】Hadoop 1.x中fsimage和edits合并实现
在NameNode运行期间,HDFS的所有更新操作都是直接写到edits中,久而久之edits文件将会变得很大:虽然这对NameNode运行时候是没有什么影响的,但是我们知道当NameNode重启的时 ...
- 使用SecureCRT 8.5快速打开sftp传输文件
一般使用Windows系统上安装的SecureCRT 8.5软件远程连接Linux服务器,通常给Linux系统传输文件或者使用FTP,或者使用SFTP等其他第三方软件,有时Linux系统上还需要做其他 ...
- 【源码解读】cycleGAN(二) :训练
源码地址:https://github.com/aitorzip/PyTorch-CycleGAN 训练的代码见于train.py,首先定义好网络,两个生成器A2B, B2A和两个判别器A, B,以及 ...
- 梯度下降算法(Gradient descent)GD
1.我们之前已经定义了代价函数J,可以将代价函数J最小化的方法,梯度下降是最常用的算法,它不仅仅用在线性回归上,还被应用在机器学习的众多领域中,在后续的课程中,我们将使用梯度下降算法最小化其他函数,而 ...
- 【问题解决方案】Xshell连接服务器并实现上传和下载文件
参考链接: Xshell连接服务器并实现上传和下载文件 第一步:xshell登录完成 略 第二步: 在服务器安装lrzsz 如果服务器的操作系统是 CentOS,则输入命令[yum install l ...