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的更多相关文章

  1. 2019CCPC网络赛 C - K-th occurrence HDU - 6704(后缀数组+ST表+二分+主席树)

    题意 求区间l,r的子串在原串中第k次出现的位置. 链接:https://vjudge.net/contest/322094#problem/C 思路 比赛的时候用后缀自动机写的,TLE到比赛结束. ...

  2. K-th occurrence HDU - 6704 (SA, 主席树)

    大意: 给定串$s$, $q$个询问$(l,r,k)$, 求子串$s[l,r]$的第$k$次出现位置. 本来是个简单签到题, 可惜比赛的时候还没学$SA$...... 好亏啊 相同的子串在$SA$中是 ...

  3. HDU - 6704 K-th occurrence (后缀数组+主席树/后缀自动机+线段树合并+倍增)

    题意:给你一个长度为n的字符串和m组询问,每组询问给出l,r,k,求s[l,r]的第k次出现的左端点. 解法一: 求出后缀数组,按照排名建主席树,对于每组询问二分或倍增找出主席树上所对应的的左右端点, ...

  4. K-th occurrence HDU - 6704 (后缀数组+二分线段树+主席树)

    大意: 给定串s, q个询问(l,r,k), 求子串s[l,r]的第kk次出现位置. 这是一篇很好的题解: https://blog.csdn.net/sdauguanweihong/article/ ...

  5. hdu 6704 K-th occurrence(后缀数组+可持久化线段树)

    Problem Description You are given a string S consisting of only lowercase english letters and some q ...

  6. HDU 6704 K-th occurrence(主席树 + RMQ + 后缀数组)题解

    题意: 给一个串\(S\),\(length\leq 1e5\),\(Q\leq1e5\)个询问,每次询问输出和\(S_lS_{l+1}\dots S_r\)长得一模一样的第\(k\)个子串的开头位置 ...

  7. HDU 5122 K.Bro Sorting(模拟——思维题详解)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5122 Problem Description Matt's friend K.Bro is an A ...

  8. HDU 5122 K.Bro Sorting(2014北京区域赛现场赛K题 模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5122 解题报告:定义一种排序算法,每一轮可以随机找一个数,把这个数与后面的比这个数小的交换,一直往后判 ...

  9. HDU 5122 K.Bro Sorting

    K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Tot ...

  10. 基础题:HDU 5122 K.Bro Sorting

    Matt's friend K.Bro is an ACMer.Yesterday, K.Bro learnt an algorithm: Bubble sort. Bubble sort will ...

随机推荐

  1. pandas DataFram的insert函数

    原文链接:https://blog.csdn.net/yanwucao/article/details/80211984 DataFrame.insert(loc, column, value, al ...

  2. 20191127 Spring Boot官方文档学习(9.1-9.3)

    9."使用方法"指南 9.1.Spring Boot应用程序 9.1.1.创建自己的FailureAnalyzer FailureAnalyzer被包装在FailureAnalys ...

  3. python+selenium切换窗口(获取句柄信息)

    current_window_handle:获得当前窗口句柄: window_handles:返回所有窗口的句柄到当前会话: switch_to.window(suoyou[0]) ========= ...

  4. TortoiseSVN-1.9.7 对应 eclipse svn 插件的 更新

    用TortoiseSVN在文件夹导入项目之后,用eclipse 导入发现没有SVN信息,无法同步工程.出现这个情况先不管 TortoiseSVN的版本,直接把eclipse的svn版本升级到最新. H ...

  5. SwipeRefreshLayout和RecyclerView类

    1 SwipeRefreshLayout和RecyclerView之间的关系 内容栏上下滚动是RecyclerView控制的,只有当内容栏滑动到最顶上时,再也拉不动了的时候,这个时候将动作交给Swip ...

  6. [知乎]自己真是Know Nothing 历史上有哪些细思恐极的事件?

    作者:敖让链接:https://www.zhihu.com/question/38374479/answer/652432284来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  7. 2019上海网络赛B题(差分 + 离散化 or 差分 + 思维)

    这题.....队里都没怎么训练差分,导致败北...写了一堆线段树嘤嘤嘤,到最后也是超时,比赛结束后看到了差分的思想于是就去学了一手. 其实了解差分思想的一眼就能看出来是差分了.但是如果对n差分的话很明 ...

  8. 分布式事务解决方案汇总:2PC、3PC、消息中间件、TCC、状态机+重试+幂等(转)

    数据一致性问题非常多样,下面举一些常见例子.比如在更新数据的时候,先更新了数据库,后更新了缓存,一旦缓存更新失败,此时数据库和缓存数据会不一致.反过来,如果先更新缓存,再更新数据库,一旦缓存更新成功, ...

  9. python字符串替换的2种方法

    python 字符串替换可以用2种方法实现:1是用字符串本身的方法.2用正则来替换字符串 下面用个例子来实验下:a = 'hello word'把a字符串里的word替换为python 1.用字符串本 ...

  10. j网络爬虫之WebMagic

    WebMagic官网:http://webmagic.io/ 注意: 1.在自定义PageProcessor中使用System.out.println(“str”),Spider.create(new ...