Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1809 Accepted Submission(s): 580
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
Source
2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛
Recommend
liuyiding | We have carefully selected several similar problems for you: 6730 6729 6728 6727 6726
题目大意:每次给出一个字串,求这个子串在原字符串中第k次出现的位置(可以重叠),如果不存在输出-1.
解题思路:由于将所有的后缀按照字典序排列后,相邻的字符串部分前缀相等,所以可以根据这个性质入手。
首先找到以l开头的后缀在所有后缀中的rank,记为pos。
由于 排名为rank1 和排名为rank2的后缀的 LCP 是 min [ height[rank+1],height[rank2]];
这个函数 从pos向两边单调递减。可以二分。
我们二分到范围之后就得到了 包含这个s(l,r)字串的所有 起点,因为要求第k个起点。
我们直接在rank在l到r 的范围内找到第k大的数即可。
写起来有很多细节,wa了很多发。。。。
主要就是求LCP的时候,忽略了l==r这种情况。。。
#include
#include
#include
#include
#include
using namespace std;
const int maxn = 100005;
const int N = 1e5+5;
/// sa[i] 排第i的是哪个串 rak[i] 第i个串排第几
/// sa[1~n]有效 rak[0~n-1]有效
/// 定义 height[i]为suffix(sa[i-1])和 suffix(sa[i]),即排名相邻的后缀的最长公共前缀
/// height[2~n]有效
///m是计数排序上限 r是要处理的数组
///n是数组长度+1,最后一个元素的后一个位置
///计数排序最小元素从1开始
/// m的范围 如果字符串只包含字母就取128 否则取最大的数字+1
int height[maxn];
int wa[maxn],wb[maxn],wv[maxn],Ws[maxn];
int rak[maxn],sa[maxn];
int s[maxn];
struct _SA
{
public:
int cmp(int *r,int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void init()
{
memset(wa,0,sizeof(wa));
memset(wb,0,sizeof(wb));
memset(wv,0,sizeof(wv));
memset(Ws,0,sizeof(Ws));
memset(rak,0,sizeof(rak));
memset(height,0,sizeof(height));
memset(sa,0,sizeof(sa));
}
void da(int *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=0; i=0; i--) sa[--Ws[x[i]]]=i;
for(p=1,j=1; p=j) y[p++]=sa[i]-j;
for(i=0; i=0; i--) sa[--Ws[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i>1;
if(pos <= m) upd(L[now], L[pre], l, m, pos);
else upd(R[now], R[pre], m+1, r, pos);
}
int query(int le,int ri,int l,int r,int k)
{
if(l==r){return l;}
int del = sum[L[ri]] - sum[L[le]];
int m = (l+r)>>1;
if(del>1;
if(LCP(mid,rak[l-1])>=len) r1 = mid-1,tmpl = mid;
else l1 = mid+1;
}
l1 = rak[l-1] ,r1 = n;
tmpr = rak[l-1];
while(l1<=r1)
{
int mid = (l1+r1)>>1;
if(LCP(rak[l-1],mid)>=len) l1 = mid+1, tmpr = mid;
else r1 = mid - 1;
}
}
void solve()
{
SA.init();
int q;
scanf("%d%d",&n,&q);
scanf("%s",s1);
for(int i=0; i