- 题意
- 题目链接
- 给一个字符串S,给q个询问,每个询问包含L和K,求原字符串长度为L的字串中字典序排在第K位的起始位置。
- 题解
- 首先处理出sa数组,然后将询问离线,按照L从大到小的顺序访问,将
rnk[0]...rnk[n−L]
加入线段树中,查询排在第k位的rnk值,利用sa即可查到起始位置。
- 代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
using namespace std;
#define ll long long
#define SZ(x) ((int)(x).size())
#define ALL(v) (v).begin(), (v).end()
#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)
#define reveach(i, v) for (__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++ i)
#define REP(i,a,n) for ( int i=a; i<int(n); i++ )
#define FOR(i,a,n) for ( int i=n-1; i>= int(a);i-- )
#define lson rt<<1, L, m
#define rson rt<<1|1, m, R
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define fi first
#define se second
#define CLR(a, b) memset(a, b, sizeof(a))
#define Max(a, b) a = max(a, b)
#define Min(a, b) b = min(a, b)
const int MAXN=1e5 + 7;
int t1[MAXN],t2[MAXN],c[MAXN];
bool cmp(int *r,int a,int b,int l)
{
return r[a] == r[b] && r[a+l] == r[b+l];
}
void da(int str[],int sa[],int rank[],int height[],int n,int m)
{
n++;
int i, j, p, *x = t1, *y = t2;
for(i = 0;i < m;i++)c[i] = 0;
for(i = 0;i < n;i++)c[x[i] = str[i]]++;
for(i = 1;i < m;i++)c[i] += c[i-1];
for(i = n-1;i >= 0;i--)sa[--c[x[i]]] = i;
for(j = 1;j <= n; j <<= 1)
{
p = 0;
for(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 < m; i++)c[i] = 0;
for(i = 0; i < n; i++)c[x[y[i]]]++;
for(i = 1; i < m;i++)c[i] += c[i-1];
for(i = n-1; i >= 0;i--)sa[--c[x[y[i]]]] = y[i];
swap(x,y);
p = 1; x[sa[0]] = 0;
for(i = 1;i < n;i++)
x[sa[i]] = cmp(y,sa[i-1],sa[i],j)?p-1:p++;
if(p >= n)break;
m = p;
}
int k = 0;
n--;
for(i = 0;i <= n;i++)rank[sa[i]] = i;
for(i = 0;i < n;i++)
{
if(k)k--;
j = sa[rank[i]-1];
while(str[i+k] == str[j+k])k++;
height[rank[i]] = k;
}
}
int rnk[MAXN], height[MAXN];
int RMQ[MAXN];
int mm[MAXN];
int best[20][MAXN];
char str[MAXN];
int r[MAXN];
int sa[MAXN];
pair<pii, int> qur[MAXN];
int ans[MAXN];
int segt[MAXN << 2];
void update(int rt, int L, int R, int idd){
if(L + 1 == R){ segt[rt] = 1; return ;}
int m = (L + R) >> 1;
if(idd < m) update(lson, idd);
else update(rson, idd);
int lc = rt << 1, rc = rt << 1 | 1;
segt[rt] = segt[lc] + segt[rc];
}
int query(int rt, int L, int R, int k){
if(L + 1 == R) return L;
int m = (L + R) >> 1;
int lc = rt << 1, rc = rt << 1 | 1;
if(k <= segt[lc]) return query(lson, k);
else return query(rson, k - segt[lc]);
}
int main(){
#ifdef ac
freopen("in.txt","r",stdin);
#endif
scanf("%s", str);
int n = strlen(str);
REP(i, 0, n + 1) r[i] = str[i];
da(r, sa, rnk, height, n, 128);
int q;
scanf("%d", &q);
REP(i, 0, q) scanf("%d%d", &qur[i].fi.fi, &qur[i].fi.se), qur[i].se = i;
sort(qur, qur + q);
int pos = 0;
FOR(i, 0, q){
int len = qur[i].fi.fi;
int k = qur[i].fi.se;
while(pos <= n - len){
update(1, 1, n + 1, rnk[pos]);
pos ++;
}
ans[qur[i].se] = query(1, 1, n + 1, k);
}
REP(i, 0, q) printf("%d\n", sa[ans[i]] + 1);
return 0;
}