Boring String Problem
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2046 Accepted Submission(s): 552
Problem Description
In this problem, you are given a string s and q queries.
For each query, you should answer that when all distinct substrings of string s were sorted lexicographically, which one is the k-th smallest.
A substring s i...j of the string s = a 1a 2 ...a n(1 ≤ i ≤ j ≤ n) is the string a ia i+1 ...a j. Two substrings s x...y and s z...w are cosidered to be distinct if s x...y ≠ S z...w
For each query, you should answer that when all distinct substrings of string s were sorted lexicographically, which one is the k-th smallest.
A substring s i...j of the string s = a 1a 2 ...a n(1 ≤ i ≤ j ≤ n) is the string a ia i+1 ...a j. Two substrings s x...y and s z...w are cosidered to be distinct if s x...y ≠ S z...w
Input
The input consists of multiple test cases.Please process till EOF.
Each test case begins with a line containing a string s(|s| ≤ 10 5) with only lowercase letters.
Next line contains a postive integer q(1 ≤ q ≤ 10 5), the number of questions.
q queries are given in the next q lines. Every line contains an integer v. You should calculate the k by k = (l⊕r⊕v)+1(l, r is the output of previous question, at the beginning of each case l = r = 0, 0 < k < 2 63, “⊕” denotes exclusive or)
Each test case begins with a line containing a string s(|s| ≤ 10 5) with only lowercase letters.
Next line contains a postive integer q(1 ≤ q ≤ 10 5), the number of questions.
q queries are given in the next q lines. Every line contains an integer v. You should calculate the k by k = (l⊕r⊕v)+1(l, r is the output of previous question, at the beginning of each case l = r = 0, 0 < k < 2 63, “⊕” denotes exclusive or)
Output
For each test case, output consists of q lines, the i-th line contains two integers l, r which is the answer to the i-th query. (The answer l,r satisfies that s
l...r is the k-th smallest and if there are several l,r available, ouput l,r which with the smallest l. If there is no l,r satisfied, output “0 0”. Note that s
1...n is the whole string)
Sample Input
aaa 4 0 2 3 5
Sample Output
1 1 1 3 1 2 0 0
Source
给出一个字符串,把所有不同的子串按字典序排序,求字典序排名第k位的子串。
先求后缀数组。因为后缀数组中的后缀已经按照字典序排序,可以根据后缀数组求出截止排名第 i 个后缀一共产生了多少个不同的子串,查询的时候二分一下。
题目要求输出满足条件的子串的起止位置,而且要求位置尽量靠左。那么从二分到的位置开始,向下一直找长度最长的后缀就好了。一直找到height比子串的长度小为止。
最开始想到用线段树查找可能的最右位置,没想到直接暴力就过了。
#include <cstdio>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=100005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
int wa[maxn],wb[maxn],wv[maxn],ws[maxn],sa[maxn],ranki[maxn],height[maxn];
int s[maxn];
ll sum[maxn];
char a[maxn];
int cmp(int *r,int a,int b,int l) {
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void build(int *r,int *sa,int n,int m) {
int i,j,k,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=0;i<m;i++) ws[i]+=ws[i-1];
for (i=n-1;i>=0;i--)
sa[--ws[x[i]]]=i;
for (j=1,p=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];
t=x;x=y;y=t;
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++;
}
for (i=1;i<n;i++) ranki[sa[i]]=i;
k=0;
for (i=0;i<n-1;height[ranki[i++]]=k) {
if (k) k--;
for (j=sa[ranki[i]-1];r[i+k]==r[j+k];k++);
}
}
int main() {
while (scanf("%s",a)!=EOF) {
int n,i,j,q;
ll k;
scanf("%d",&q);
n=strlen(a);
for (i=0;i<n;i++)
s[i]=a[i]-'a'+1;
s[n]=0;
build(s,sa,n+1,27);
sum[0]=0;sum[1]=n-sa[1];
for (i=2;i<=n;i++)
sum[i]=sum[i-1]+n-sa[i]-height[i];
int l,r,len;
l=r=0;
for (i=1;i<=q;i++) {
scanf("%lld",&k);
k=(l^r^k)+1;
if (k>sum[n]) {
l=r=0;
printf("0 0\n");
} else {
int pos=lower_bound(sum+1,sum+n,k)-sum;
l=sa[pos]+1;len=k-sum[pos-1]+height[pos];
for (j=pos+1;j<=n;j++) {
if (height[j]<len) break;
l=min(l,sa[j]+1);
}
r=l+len-1;
printf("%d %d\n",l,r);
}
}
}
return 0;
}