K-th occurrence 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 (后缀数组+主席树+RMQ)

Problem 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 SlSl+1...Sr 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≤105),Q(1≤Q≤105), 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 103.

 

 

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

题目大意:给出一个字符串,q次询问,每次询问l, r, k.就是l~r这段区间的字串在文本串中出现第k次的位置。

解题思路:后缀数组求解。在height数组上找到一段大于所要查找的字符串长度的区间。所找到的这个区间在sa数组中对应区间做一个主席树查询区间第k大就可以了。比较麻烦的是在用RMQ找区间左右边界的情况

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值