[SPOJ7258]Lexicographical Substring Search
[SPOJ7258]Lexicographical Substring Search
试题描述
Little Daniel loves to play with strings! He always finds different ways to have fun with strings! Knowing that, his friend Kinan decided to test his skills so he gave him a string S and asked him Q questions of the form:
If all distinct substrings of string S were sorted lexicographically, which one will be the K-th smallest?
After knowing the huge number of questions Kinan will ask, Daniel figured out that he can't do this alone. Daniel, of course, knows your exceptional programming skills, so he asked you to write him a program which given S will answer Kinan's questions.
Example:
S = "aaa" (without quotes)
substrings of S are "a" , "a" , "a" , "aa" , "aa" , "aaa". The sorted list of substrings will be:
"a", "aa", "aaa".
输入
输出
Output consists of Q lines, the i-th contains a string which is the answer to the i-th asked question.
输入示例
aaa
输出示例
aa
aaa
题解
这题其实就是这道题的一个子问题。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std; int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 90010 char S[maxn];
int n, rank[maxn], height[maxn], sa[maxn], Ws[maxn]; bool cmp(int* a, int p1, int p2, int l) {
if(p1 + l > n && p2 + l > n) return a[p1] == a[p2];
if(p1 + l > n || p2 + l > n) return 0;
return a[p1] == a[p2] && a[p1+l] == a[p2+l];
}
void ssort() {
int *x = rank, *y = height;
int m = 0;
for(int i = 1; i <= n; i++) Ws[x[i] = S[i]]++, m = max(m, x[i]);
for(int i = 1; i <= m; i++) Ws[i] += Ws[i-1];
for(int i = n; i; i--) sa[Ws[x[i]]--] = i;
for(int j = 1, pos = 0; pos < n; j <<= 1, m = pos) {
pos = 0;
for(int i = n - j + 1; i <= n; i++) y[++pos] = i;
for(int i = 1; i <= n; i++) if(sa[i] > j) y[++pos] = sa[i] - j;
for(int i = 1; i <= m; i++) Ws[i] = 0;
for(int i = 1; i <= n; i++) Ws[x[i]]++;
for(int i = 1; i <= m; i++) Ws[i] += Ws[i-1];
for(int i = n; i; i--) sa[Ws[x[y[i]]]--] = y[i];
swap(x, y); pos = 1; x[sa[1]] = 1;
for(int i = 2; i <= n; i++) x[sa[i]] = cmp(y, sa[i], sa[i-1], j) ? pos : ++pos;
}
return ;
}
void calch() {
for(int i = 1; i <= n; i++) rank[sa[i]] = i;
for(int i = 1, j, k = 0; i <= n; height[rank[i++]] = k)
for(k ? k-- : 0, j = sa[rank[i]-1]; S[j+k] == S[i+k]; k++);
return ;
} int en[maxn]; int main() {
scanf("%s", S + 1);
n = strlen(S + 1); ssort();
calch();
for(int i = 1; i <= n; i++) en[i] = n - sa[i] + 1 - height[i];
for(int i = 1; i <= n; i++) en[i] += en[i-1];
int q = read();
while(q--) {
int k = read(), p = lower_bound(en + 1, en + n + 1, k) - en;
int l = sa[p], r = n - (en[p] - k);
for(int i = l; i <= r; i++) putchar(S[i]); putchar('\n');
} return 0;
}
这道题用后缀自动机也是可以滴。对于每个节点 i 我们维护一发 f(i) 表示状态 i 之后总共有多少种填法,然后我们每一位枚举填哪个字母,看后续状态数是否 ≥ k。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
using namespace std; int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 180010
#define maxa 26 char S[maxn];
int n; int rt, last, ToT, ch[maxn][maxa], par[maxn], Max[maxn], f[maxn];
void extend(int x) {
int p = last, np = ++ToT; Max[np] = Max[p] + 1; f[np] = 1; last = np;
while(p && !ch[p][x]) ch[p][x] = np, p = par[p];
if(!p){ par[np] = rt; return ; }
int q = ch[p][x];
if(Max[q] == Max[p] + 1){ par[np] = q; return ; }
int nq = ++ToT; Max[nq] = Max[p] + 1; f[nq] = 1;
memcpy(ch[nq], ch[q], sizeof(ch[q]));
par[nq] = par[q];
par[q] = par[np] = nq;
while(p && ch[p][x] == q) ch[p][x] = nq, p = par[p];
return ;
}
int sa[maxn], Ws[maxn];
void build() {
for(int i = 1; i <= ToT; i++) Ws[n-Max[i]]++;
for(int i = 1; i <= n; i++) Ws[i] += Ws[i-1];
for(int i = ToT; i; i--) sa[Ws[n-Max[i]]--] = i;
for(int i = 1; i <= ToT; i++)
for(int c = 0; c < maxa; c++) f[sa[i]] += f[ch[sa[i]][c]];
return ;
} int main() {
scanf("%s", S); n = strlen(S);
rt = last = ToT = 1;
for(int i = 0; i < n; i++) extend(S[i] - 'a');
build();
// for(int i = 1; i <= ToT; i++) printf("%d%c", f[i], i < ToT ? ' ' : '\n'); int q = read();
while(q--) {
int k = read(), p = rt;
while(k)
for(int c = 0; c < maxa; c++) if(ch[p][c])
if(f[ch[p][c]] >= k) {
putchar(c + 'a'); k--; p = ch[p][c]; break;
}
else k -= f[ch[p][c]];
putchar('\n');
} return 0;
}
[SPOJ7258]Lexicographical Substring Search的更多相关文章
- 2018.12.22 spoj7258 Lexicographical Substring Search(后缀自动机)
传送门 samsamsam基础题. 题意简述:给出一个串,询问第kkk大的本质不同的串. 然而这就是弦论的简化版. 我们把samsamsam建出来然后贪心选择就行了. 代码: #include< ...
- spoj 7258 Lexicographical Substring Search (后缀自动机)
spoj 7258 Lexicographical Substring Search (后缀自动机) 题意:给出一个字符串,长度为90000.询问q次,每次回答一个k,求字典序第k小的子串. 解题思路 ...
- SPOJ SUBLEX 7258. Lexicographical Substring Search
看起来像是普通的SAM+dfs...但SPOJ太慢了......倒腾了一个晚上不是WA 就是RE ..... 最后换SA写了...... Lexicographical Substring Searc ...
- SPOJ SUBLEX - Lexicographical Substring Search 后缀自动机 / 后缀数组
SUBLEX - Lexicographical Substring Search Little Daniel loves to play with strings! He always finds ...
- Lexicographical Substring Search SPOJ - SUBLEX (后缀数组)
Lexicographical Substrings Search \[ Time Limit: 149 ms \quad Memory Limit: 1572864 kB \] 题意 给出一个字符串 ...
- Lexicographical Substring Search SPOJ - SUBLEX (后缀自动机)
Lexicographical Substrings Search \[ Time Limit: 149 ms \quad Memory Limit: 1572864 kB \] 题意 给出一个字符串 ...
- SPOJ7258 SUBLEX - Lexicographical Substring Search(后缀自动机)
Little Daniel loves to play with strings! He always finds different ways to have fun with strings! K ...
- SPOJ7258 SUBLEX - Lexicographical Substring Search
传送门[洛谷] 心态崩了我有妹子 靠 我写的记忆化搜索 莫名WA了 然后心态崩了 当我正要改成bfs排序的时候 我灵光一动 md我写的i=0;i<25;i++??? 然后 改过来就A掉了T^T ...
- 【SPOJ 7258】Lexicographical Substring Search
http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...
随机推荐
- Person p = new Person("zhangsan",20);该句话都做了什么事情?
1,因为new用到了Person.class.所以会先找到Person.class文件并加载到内存中.2,执行该类中的static代码块,如果有的话,给Person.class类进行初始化.3,在堆内 ...
- static属性
static 属于全局,也就是类的属性 和方法,换句话说 一个类,不管有多少个实例,却只有一个全局变量 用static修饰的属性和方法称为静态属性和方法 需要注意的是 静态属性和方法属于类方法,加载类 ...
- 对char类型的数组进行冒泡排序
package maopaopaixu; import java.util.Arrays; import java.util.Scanner; public class Demo02 { public ...
- Kali linux 2016.2(Rolling)里安装中文输入法
写在前面的话 关于中文输入法,实在是有太多了.当然,你也不可以不安装,(安装了增强工具即可),在windows 里输入中文,复制进去即可. 但是呢,想成为高手,还是要学会安装和使用各版本的中文输入法. ...
- Spring日期格式初始化
原文链接地址 一.初始化参数绑定[一种日期格式] 配置步骤: ①:在applicationcontext.xml中只需要配置一个包扫描器即可 1 2 <!-- 包扫描器 --> ...
- 【开源】基于EF6+MVC5+API2+Easyui1.4.5+Easyui管理模板开发的管理系统
经过近一步完善调整,现将本系统源码正式开放,定名为:EasyuiAdminFramework,另外EasyuiAdminTemplate及EasyuiFlatTheme也一并开源 项目主页:http: ...
- 锁 Lock、重入锁、写入锁
ReentrantLock 重入锁 类似于synchronize 区别与写法上,在需要进行同步的代码部分加上锁定,但不要忘记最后一定要释放锁定, 不然会造成锁永远无法释放,其他线程永远进不来的结果.e ...
- 修改vim注释字体颜色
vim /etc/vimrc#最后一行新增代码块hi comment ctermfg=6 注:注释用 " 标注 PS:默认的注释颜色是4 然后有0,1,2,3,4,5,6,7来选择.可以除 ...
- API设计指南(译)
API的设计在软件系统中的重要性不言而喻,在swift.org上看到一篇“API Design Guidelines”,虽然是就Swift而言,但对于其它语言也有不少可以借鉴的地方,在这里粗略翻译一二 ...
- Vue 路由知识二(工程模式下路由的配置)
vue-router是一个插件包,所以我们还是需要用npm/cnpm来进行安装的:npm/cnpm install vue-router --save-dev. 在路由的核心文件:src/router ...