Boring String Problem

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1848    Accepted Submission(s): 492

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 si...j of the string s = a1a2 ...an(1 ≤ i ≤ j ≤ n) is the string aiai+1 ...aj. Two substrings sx...y and sz...w are cosidered to be distinct if sx...y ≠ Sz...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| ≤ 105) with only lowercase letters.

Next line contains a postive integer q(1 ≤ q ≤ 105), 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 < 263, “⊕” 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 sl...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 s1...n is the whole string)
 
Sample Input
aaa
4
0
2
3
5
 
Sample Output
1 1
1 3
1 2
0 0
/*
hdu 5008 查找字典序第k小的子串 problem:
给你一个字符串,每次寻找字典序第k小的子串(不同的)的开始,结尾坐标。 solve:
首先可以知道height本身就是按字典序来弄的,对于sa[i-1]和sa[i]而言只要减去
它们的公共部分即height[i],就是不同的子串.
可以借此处理出来[1,n]即到字典序第i大的后缀时总共有多少个不同的子串.然后
利用二分查找就能找到位置
还要注意就是当有多个的时候输出最小的位置,可以判断当前子串是否是公共前缀的
一部分.往后枚举找出最小的位置即可
a
aa
aaa
当你要找最小的串a时可能会找到 3 3.因为它也是aa,aaa的一部分,所以需要枚举一下 hhh-2016-08-12 19:24:38
*/
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
using namespace std;
#define lson (i<<1)
#define rson ((i<<1)|1)
typedef long long ll;
typedef unsigned int ul;
const int INF = 0x3f3f3f3f;
const int maxn = 100000+10;
const int mod = 1e9+7; int t1[maxn],t2[maxn],c[maxn];
bool cmp(int *r,int a,int b,int l)
{
return r[a]==r[b] &&r[l+a] == r[l+b];
} void get_sa(int str[],int sa[],int Rank[],int height[],int n,int m)
{
n++;
int p,*x=t1,*y=t2;
for(int i = 0; i < m; i++) c[i] = 0;
for(int i = 0; i < n; i++) c[x[i] = str[i]]++;
for(int i = 1; i < m; i++) c[i] += c[i-1];
for(int i = n-1; i>=0; i--) sa[--c[x[i]]] = i;
for(int j = 1; j <= n; j <<= 1)
{
p = 0;
for(int i = n-j; i < n; i++) y[p++] = i;
for(int i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i]-j;
for(int i = 0; i < m; i++) c[i] = 0;
for(int i = 0; i < n; i++) c[x[y[i]]]++ ;
for(int i = 1; i < m; i++) c[i] += c[i-1];
for(int i = n-1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i]; swap(x,y);
p = 1;
x[sa[0]] = 0;
for(int 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(int i = 0; i <= n; i++)
Rank[sa[i]] = i;
for(int i = 0; i < n; i++)
{
if(k) k--;
int j = sa[Rank[i]-1];
while(str[i+k] == str[j+k]) k++;
height[Rank[i]] = k;
} } int Rank[maxn],height[maxn];
int sa[maxn];
char str[maxn];
int r[maxn];
ll num[maxn];
int lpos,rpos;
int len ; void fin(ll x)
{
int l =1 ,r = len;
int cur = 0;
while(l <= r)
{
int mid = (l+r) >> 1;
if(num[mid] >= x)
{
cur = mid;
r = mid-1;
}
else
l = mid+1;
}
// cout << "cur:" <<cur <<endl;
x = x - num[cur-1];
lpos = sa[cur]+1;
rpos = sa[cur]+height[cur]+x;
int tlen = rpos-lpos+1;
if(cur+1 <= len && tlen <= height[cur+1])
{
for(int i = cur + 1; i <= len; i++)
{
if(height[i] >= tlen)
{
if(lpos > sa[i]+1)
{
lpos = sa[i]+1;
rpos = lpos+tlen-1;
}
}
else
break;
}
}
return ;
} int main()
{
while(scanf("%s",str) != EOF)
{
len = strlen(str);
for(int i = 0; i <len; i ++)
{
r[i] = str[i];
}
r[len] = 0;
num[0] = 0;
get_sa(r,sa,Rank,height,len,200);
for(int i = 1; i <= len; i++)
{
num[i] = (ll)(len - sa[i] - height[i]);
num[i] += num[i-1];
}
int n;
ll x;
scanf("%d",&n);
lpos = 0,rpos = 0;
for(int i = 1; i <= n; i++)
{
scanf("%I64d",&x);
ll k = (ll)(lpos^rpos^x)+1LL;
// cout << "k:" << k <<endl;
lpos = rpos = 0;
if(k > num[len])
{
printf("0 0\n");
lpos = 0,rpos = 0;
continue;
}
fin(k);
printf("%d %d\n",lpos,rpos);
}
}
return 0;
}

  

hdu 5008 查找字典序第k小的子串的更多相关文章

  1. Leetcode 440.字典序第k小的数字

    字典序第k小的数字 给定整数 n 和 k,找到 1 到 n 中字典序第 k 小的数字. 注意:1 ≤ k ≤ n ≤ 109. 示例 : 输入: n: 13 k: 2 输出: 10 解释: 字典序的排 ...

  2. 后缀自动机求字典序第k小的串——p3975

    又领悟到了一点新的东西,后缀自动机其实可以分为两个数据结构,一个是后缀树,还有一个是自动机 后缀树用来划分endpos集合,并且维护后缀之间的关系,此时每个结点代表的是一些后缀相同且长度连续的子串 自 ...

  3. poj2828(线段树查找序列第k小的值)

    题目链接:https://vjudge.net/problem/POJ-2828 题意:有n个人,依次给出这n个人进入队列时前面有多少人p[i],和它的权值v[i],求最终队列的权值序列. 思路:基本 ...

  4. 【LeetCode】1415. 长度为 n 的开心字符串中字典序第 k 小的字符串 The k-th Lexicographical String of All Happy Strings of Le

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetcod ...

  5. [Swift]LeetCode440. 字典序的第K小数字 | K-th Smallest in Lexicographical Order

    Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n. N ...

  6. 440 K-th Smallest in Lexicographical Order 字典序的第K小数字

    给定整数 n 和 k,找到 1 到 n 中字典序第 k 小的数字.注意:1 ≤ k ≤ n ≤ 109.示例 :输入:n: 13   k: 2输出:10解释:字典序的排列是 [1, 10, 11, 1 ...

  7. Java实现 LeetCode 440 字典序的第K小数字

    440. 字典序的第K小数字 给定整数 n 和 k,找到 1 到 n 中字典序第 k 小的数字. 注意:1 ≤ k ≤ n ≤ 109. 示例 : 输入: n: 13 k: 2 输出: 10 解释: ...

  8. 440. 字典序的第K小数字 + 字典树 + 前缀 + 字典序

    440. 字典序的第K小数字 LeetCode_440 题目描述 方法一:暴力法(必超时) package com.walegarrett.interview; /** * @Author WaleG ...

  9. HDU 5008 Boring String Problem

    题意:给定一个串长度<=1e5,将其所有的不同的字串按照字典序排序,然后q个询问,每次询问字典序第k小的的起始坐标,并且起始坐标尽量小. 分析: 一开始看错题意,没有意识到是求不同的字串中第k小 ...

随机推荐

  1. C语言--第14.15周作业

    一. 7-3 将数组中的数逆序存放 1.代码 #include 2<stdio.h> int main() { int a[10]; int i, n, s; scanf("%d ...

  2. Solaris 11 system package 安装与更新(如:assembler)

    最近在VirtualBox虚拟机中导入了Solaris 11.3.在里面安装Oracle数据库时,先行条件检查没通过,提示缺少程序包assembler. 在网上看了许多,这方面的信息还比较少.最后在O ...

  3. Linq 生成运算符 Empty,Range,Repeat

    var c1 = Enumerable.Empty<string>();//c1.Count=0 , );//{9527,9528,9529,......9536} , );//{9527 ...

  4. Docker学习笔记 - Docker的简介

    传统硬件虚拟化:虚拟硬件,事先分配资源,在虚拟的硬件上安装操作系统,虚拟机启动起来以后资源就会被完全占用. 操作系统虚拟化:docker是操作系统虚拟化,借助操作系统内核特性(命名空间.cgroups ...

  5. SpringBoot 概念和起步

    一.概念和由来 1.什么是 Spring Boot Spring Boot 的设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用特定方式来进行配置,从而使开发人员不再需要定义样板化 ...

  6. Windows10下的docker安装与入门 (一)使用docker toolbox安装docker

    Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相互之间不会有任何 ...

  7. SpringMVC(十一):SpringMVC 处理输出模型数据之SessionAttributes

    Spring MVC提供了以下几种途径输出模型数据:1)ModelAndView:处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据:2)Map及Model:处理方法入参 ...

  8. linux系统环境与文件权限

    默认有6个命令交互通道和一个图形界面交互通道,默认进入到的是图形界面通道 命令交互模式切换:ctrl+alt+f1---f6 图形交互界面 ctrl+alt+f7 1.图形界面交互模式 - termi ...

  9. 对scrapy经典框架爬虫原理的理解

    1,spider打开某网页,获取到一个或者多个request,经由scrapy engine传送给调度器schedulerrequest特别多并且速度特别快会在scheduler形成请求队列queue ...

  10. Spring-Cloud(三)Eureka注册中心实现高可用

    前言: spring-cloud为基础的微服务架构,所有的微服务都需要注册到注册中心,如果这个注册中心阻塞或者崩了,那么整个系统都无法继续正常提供服务,所以,这里就需要对注册中心进行集群,换言之,高可 ...