HDU-6704 K-th occurrence
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 $S_lS_{l+1}...S_r $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≤\(10^5\)),Q(1≤Q≤\(10^5\)), 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 \(10^3\).
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
题解
给定一个字符串,每次询问[l,r]的字符串第k次出现的位置,没有则输出-1
后缀数组理解深刻的话应该可以秒掉这道题
首先,height[i]表示排名第i位的和第i-1位的最长公共前缀,所以我们要找某个子串出现的所有位置,只需要在height数组中二分,询问的字串所处后缀的排名即为\(rk[l]\),那么我们从\(rk[l]\)开始向上向下二分,让这段的区间height最小值大于r-l+1,那么他们就都有r-l+1的最长公共前缀,我们找出这个边界后,用主席树求这个区间中sa数组的第k大即可。
AC代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
typedef long long ll;
char s[N];
int x[N], y[N], c[N], sa[N], rk[N], height[N];
int n, m, q;
void tsort() {
    for (int i = 0; i <= m; i++) c[i] = 0;
    for (int i = 1; i <= n; i++) c[x[i]]++;
    for (int i = 2; i <= m; i++) c[i] += c[i - 1];
    for (int i = n; i >= 1; i--) sa[c[x[y[i]]]--] = y[i];
}
void get_sa() {
    memset(c, 0, sizeof(c));
    memset(x, 0, sizeof(x));
    memset(y, 0, sizeof(y));
    for (int i = 1; i <= n; i++) x[i] = s[i], y[i] = i;
    tsort();
    for (int k = 1; k <= n; k <<= 1) {
        int num = 0;
        for (int i = n - k + 1; i <= n; i++) y[++num] = i;
        for (int i = 1; i <= n; i++) if (sa[i] > k) y[++num] = sa[i] - k;
        tsort();
        swap(x, y);
        x[sa[1]] = 1;
        num = 1;
        for (int i = 2; i <= n; i++)
            x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) ? num : ++num;
        if (num == n) break;
        m = num;
    }
}
void get_h() {
    int k = 0;
    for (int i = 1; i <= n; i++) rk[sa[i]] = i;
    for (int i = 1; i <= n; i++) {
        if (rk[i] == 1) continue;
        if (k) k--;
        int j = sa[rk[i] - 1];
        while (s[i + k] == s[j + k]) k++;
        height[rk[i]] = k;
    }
}
int st[N][20], lg2[N];
void ST() {
    for (int i = 1; i <= n; i++) {
        st[i][0] = height[i];
    }
    for (int j = 1; (1 << j) <= n; j++) {
        for (int i = 1; (i + (1 << j) - 1) <= n; i++) {
            st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
        }
    }
    for (int i = 2; i <= n; i++) {
        lg2[i] = lg2[i >> 1] + 1;
    }
}
int rmq(int l, int r) {
    if (l > r)
        return 0;
    else {
        int x = lg2[r - l + 1];
        return min(st[l][x], st[r - (1 << x) + 1][x]);
    }
}
int L[N * 40], R[N * 40], T[N], cnt;
ll sum[N * 40];
int build(int l, int r) {
    int rt = ++cnt;
    sum[rt] = 0;
    int mid = (l + r) >> 1;
    if (l < r) {
        L[rt] = build(l, mid);
        R[rt] = build(mid + 1, r);
    }
    return rt;
}
int update(int pre, int l, int r, int x) {
    int rt = ++cnt;
    int mid = (l + r) >> 1;
    L[rt] = L[pre], R[rt] = R[pre], sum[rt] = sum[pre] + 1;
    if (l < r) {
        if (x <= mid) L[rt] = update(L[pre], l, mid, x);
        else R[rt] = update(R[pre], mid + 1, r, x);
    }
    return rt;
}
int query(int u, int v, int l, int r, int k) {
    if (l >= r) {
        return l;
    }
    int mid = (l + r) >> 1;
    int x = sum[L[v]] - sum[L[u]];
    if (x >= k) return query(L[u], L[v], l, mid, k);
    else {
        if (sum[R[v]] - sum[R[u]] < k - x) return -1;
        return query(R[u], R[v], mid + 1, r, k - x);
    }
}
int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        m = 130;
        scanf("%d%d", &n, &q);
        scanf("%s", s + 1);
        cnt = 0;
        get_sa();
        get_h();
        ST();
        T[0] = build(1, n);
        for (int i = 1; i <= n; i++) {
            T[i] = update(T[i - 1], 1, n, sa[i]);
        }
        while (q--) {
            int l, r, k;
            scanf("%d%d%d", &l, &r, &k);
            int tl = rk[l], tr = rk[l];
            int x = 1, y = rk[l];
            while (x <= y) {
                int mid = (x + y) >> 1;
                if (rmq(mid, rk[l]) >= r - l + 1) {
                    y = mid - 1;
                    tl = min(tl, mid - 1);//注意细节
                }
                else x = mid + 1;
            }
            x = rk[l] + 1, y = n;//注意细节
            while (x <= y) {
                int mid = (x + y) >> 1;
                if (rmq(rk[l] + 1, mid) >= r - l + 1) {
                    x = mid + 1;
                    tr = max(tr, mid);
                }
                else y = mid - 1;
            }
            printf("%d\n", query(T[tl - 1], T[tr], 1, n, k));
        }
    }
    return 0;
}
HDU-6704 K-th occurrence的更多相关文章
- 2019CCPC网络赛 C - K-th occurrence HDU - 6704(后缀数组+ST表+二分+主席树)
		题意 求区间l,r的子串在原串中第k次出现的位置. 链接:https://vjudge.net/contest/322094#problem/C 思路 比赛的时候用后缀自动机写的,TLE到比赛结束. ... 
- K-th occurrence HDU - 6704 (SA, 主席树)
		大意: 给定串$s$, $q$个询问$(l,r,k)$, 求子串$s[l,r]$的第$k$次出现位置. 本来是个简单签到题, 可惜比赛的时候还没学$SA$...... 好亏啊 相同的子串在$SA$中是 ... 
- HDU - 6704 K-th occurrence (后缀数组+主席树/后缀自动机+线段树合并+倍增)
		题意:给你一个长度为n的字符串和m组询问,每组询问给出l,r,k,求s[l,r]的第k次出现的左端点. 解法一: 求出后缀数组,按照排名建主席树,对于每组询问二分或倍增找出主席树上所对应的的左右端点, ... 
- K-th occurrence HDU - 6704 (后缀数组+二分线段树+主席树)
		大意: 给定串s, q个询问(l,r,k), 求子串s[l,r]的第kk次出现位置. 这是一篇很好的题解: https://blog.csdn.net/sdauguanweihong/article/ ... 
- hdu 6704 K-th occurrence(后缀数组+可持久化线段树)
		Problem Description You are given a string S consisting of only lowercase english letters and some q ... 
- HDU 6704 K-th occurrence(主席树 + RMQ + 后缀数组)题解
		题意: 给一个串\(S\),\(length\leq 1e5\),\(Q\leq1e5\)个询问,每次询问输出和\(S_lS_{l+1}\dots S_r\)长得一模一样的第\(k\)个子串的开头位置 ... 
- HDU 5122 K.Bro Sorting(模拟——思维题详解)
		题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5122 Problem Description Matt's friend K.Bro is an A ... 
- HDU 5122 K.Bro Sorting(2014北京区域赛现场赛K题 模拟)
		题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5122 解题报告:定义一种排序算法,每一轮可以随机找一个数,把这个数与后面的比这个数小的交换,一直往后判 ... 
- HDU 5122 K.Bro Sorting
		K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Tot ... 
- 基础题:HDU 5122 K.Bro Sorting
		Matt's friend K.Bro is an ACMer.Yesterday, K.Bro learnt an algorithm: Bubble sort. Bubble sort will ... 
随机推荐
- powershell下载网站图片
			$picurl = "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=10" $data = ... 
- selenium学习-对当前浏览器窗口截屏
			方法:get_screenshot_as_file(filename) # coding=UTF-8 #16.对当前浏览器窗口截屏 import sys reload(sys) sys.setdefa ... 
- [转帖]22款让Kubernetes锦上添花的开源工具
			22款让Kubernetes锦上添花的开源工具 http://soft.zhiding.cn/software_zone/2019/0506/3117650.shtml 找时间尝试一下. 至顶网软件频 ... 
- Julia出现错误ERROR: LoadError: syntax: try without catch or finally
			因项目要求进行机器学习数据可视化,要求尝试使用Julia,在此,记录下遇到的坑,仅为记录效果.后续陆续更新. 问题一:关于LightML库中的坑:ERROR: LoadError: syntax: t ... 
- Android的Monkey和MonkeyRunner
			本文部分解释性语段摘自网络百科或其它BLOG,语句内容网络随处可见,也不知道谁是初始原创,便不再署名出处,如有雷同,还请见谅. Monkey 什么是Monkey Monkey是Android中的一个命 ... 
- 动态规划: HDU1003Max Sum
			Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ... 
- ☆☆☆☆☆Placeholder兼容各大浏览器的例子☆☆☆☆☆
			<!doctype html> <html> <head> <meta charset="utf-8"> <title> ... 
- spark 在启动的时候出现JAVA_HOME not set
			解决方法:在sbin目录下的spark-config.sh 中添加对应的jdk 路径,然后使用scp -r 命令复制到各个worker节点 
- Python 操作sqlite数据库及保存查询numpy类型数据(二)
			# -*- coding: utf-8 -*- ''' Created on 2019年3月6日 @author: Administrator ''' import sqlite3 import nu ... 
- svn版本服务器的搭建和简单使用
			⼀ 服务器搭建篇 1 在”应⽤用程序”⽂文件夹下,找到”实⽤用⼯工具”,打开”终端”APP 2 运⾏行svnadmin create repository,运⾏行完毕之后,可以在当前⺫⽬目录下找 到⼀ ... 
