2019中國大學生程序設計競賽(CCPC) - 網絡選拔賽 K-th occurrence (後綴數組+主席樹+RMQ)

K-th occurrence

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<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<iostream>
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<m; i++) Ws[i]=0;
        for(i=0; i<n; i++) Ws[x[i]=r[i]]++;
        for(i=1; i<m; i++) Ws[i]+=Ws[i-1];
        for(i=n-1; i>=0; i--) sa[--Ws[x[i]]]=i;
        for(p=1,j=1; p<n; j*=2,m=p)
        {
            for(p=0,i=n-j; i<n; i++) y[p++]=i;
            for(i=0; i<n; i++) if(sa[i]>=j) y[p++]=sa[i]-j;
            for(i=0; i<n; i++) wv[i]=x[y[i]];
            for(i=0; i<m; i++) Ws[i]=0;
            for(i=0; i<n; i++) Ws[wv[i]]++;
            for(i=1; i<m; i++) Ws[i]+=Ws[i-1];
            for(i=n-1; 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<n; i++ )
                x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
        }
    }

    void calheight(int *r,int *sa,int n)
    {
        int i,j,k=0;
        for(i=1; i<=n; i++)
            rak[sa[i]]=i;
        for(i=0; i<n; height[rak[i++]]=k)
            for(k?k--:0,j=sa[rak[i]-1]; r[i+k]==r[j+k]; k++);
    }

}SA;

int tot;
int T[N];
int sum[N*20],L[N*20],R[N*20];

void upd(int &now, int pre,int l,int r,int pos)
{
    now = ++tot;
    sum[now] = sum[pre]+1;
    L[now] = L[pre];
    R[now] = R[pre];
    if(l==r)return ;
    int m = (l+r)>>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<k) return query(R[le], R[ri], m+1, r,k-del);
    else return query(L[le],L[ri], l, m, k);
}


char s1[maxn];
int tmpl,tmpr;
int a[N][20];
int n;

void RMQ(int n)
{
    for(int i=1; i<=n; i++)
    {
        a[i][0] = height[i];
    }
    for(int i=1; (1<<i)<=n; i++)
    {
        for(int j=1; j+(1<<i)-1<=n; j++)
        {
            a[j][i] = min(a[j][i-1], a[j+(1<<(i-1))][i-1]);
        }
    }
}

int LCP(int l,int r)
{
    if(l==r)return n - sa[l];///l==r時返回長度
    l++;
    int k = log2(r-l+1);
    return min(a[l][k], a[(r-(1<<k))+1][k]);
}

void _find(int l,int n,int len)
{
    int l1=1, r1 = rak[l-1];
    tmpl = rak[l-1];
    while(l1<=r1)
    {
        int mid = (l1+r1)>>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<n; i++) s[i] = s1[i] - 'a' +1;
    s[n]=0;
    SA.da(s, sa, n+1, 30);
    SA.calheight(s, sa, n);
    tot=0;
    for(int i=1; i<=n; i++)upd(T[i],T[i-1],1,n,sa[i]+1);
    RMQ(n);

    while(q--)
    {
        int l,r,k;
        scanf("%d%d%d",&l,&r,&k);
        _find(l,n,r-l+1);
        if(tmpr-tmpl+1 < k) puts("-1");
        else printf("%d\n",query(T[tmpl-1],T[tmpr],1,n,k));
    }
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)solve();
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章