【SPOJ 7258】Lexicographical Substring Search
http://www.spoj.com/problems/SUBLEX/
好难啊。
建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量。该状态能走到的所有状态的f值的和+1就是当前状态的f值。
最后对于询问的k,从root开始走顺便加加减减就可以了。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int in() {
int k = 0; char c = getchar();
for(; c < '0' || c > '9'; c = getchar());
for(; c >= '0' && c <= '9'; c = getchar())
k = k * 10 + c - 48;
return k;
}
int tot = 0, par[250003], go[250003][26], val[250003], f[250003], root, last;
void extend(int w) {
int p = last;
int np = ++tot; val[np] = val[p] + 1;
while (p && go[p][w] == 0)
go[p][w] = np, p = par[p];
if (p == 0) par[np] = root;
else {
int q = go[p][w];
if (val[q] == val[p] + 1) par[np] = q;
else {
int nq = ++tot; val[nq] = val[p] + 1;
memcpy(go[nq], go[q], sizeof(go[q]));
par[nq] = par[q];
par[q] = par[np] = nq;
while (p && go[p][w] == q)
go[p][w] = nq, p = par[p];
}
}
last = np;
}
char s[150003];
int len, Q, k, c[150003], id[250003], tmp;
int main() {
root = last = ++tot;
scanf("%s", s + 1);
len = strlen(s + 1);
for(int i = 1; i <= len; ++i)
extend(s[i] - 'a');
for(int i = 1; i <= tot; ++i)
++c[val[i]];
for(int i = 1; i <= len; ++i)
c[i] += c[i - 1];
for(int i = tot; i >= 1; --i)
id[c[val[i]]--] = i;
int sum, x;
for(int i = tot; i >= 1; --i) {
sum = 0; x = id[i];
for(int j = 0; j < 26; ++j)
sum += f[go[x][j]];
f[x] = sum + 1;
}
Q = in();
while (Q--) {
k = in();
tmp = root;
while (k) {
for(int i = 0; i < 26; ++i)
if (x = go[tmp][i])
if (f[x] >= k) {
putchar('a' + i);
--k;
tmp = x;
break;
} else
k -= f[x];
}
puts("");
}
return 0;
}
【SPOJ 7258】Lexicographical Substring Search的更多相关文章
- 【spoj SUBLEX】 Lexicographical Substring Search
http://www.spoj.com/problems/SUBLEX/ (题目链接) 题意 给出一个字符串,询问其中字典序第K小的子串. Solution 后缀自动机例题. 构出后缀自动机以后,对每 ...
- 【SPOJ - SUBLEX】Lexicographical Substring Search 【后缀自动机+dp】
题意 给出一个字符串和q个询问,每个询问给出一个整数k,输出第k大得子串. 分析 建后缀自动机,利用匹配边来解决.设d[v]为从状态v开始有多少不同的路径.这个显然是可以递推出来的.然后对于每个询问, ...
- 【spoj7528】 Lexicographical Substring Search
http://www.spoj.com/problems/SUBLEX/ (题目链接) 题意 给出一个字符串,询问其中字典序第K小的子串. Solution 后缀自动机例题. 构出后缀自动机以后,对每 ...
- SPOJ:SUBLEX - Lexicographical Substring Search
题面 第一行给定主串\((len<=90000)\) 第二行给定询问个数\(T<=500\) 随后给出\(T\)行\(T\)个询问,每次询问排名第\(k\)小的串,范围在\(int\)内 ...
- 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 ...
- 【 SPOJ - GRASSPLA】 Grass Planting (树链剖分+树状数组)
54 种草约翰有 N 个牧场,编号为 1 到 N.它们之间有 N − 1 条道路,每条道路连接两个牧场.通过这些道路,所有牧场都是连通的.刚开始的时候,所有道路都是光秃秃的,没有青草.约翰会在一些道 ...
- [SPOJ7258]Lexicographical Substring Search
[SPOJ7258]Lexicographical Substring Search 试题描述 Little Daniel loves to play with strings! He always ...
随机推荐
- Stanford机器学习笔记-6. 学习模型的评估和选择
6. 学习模型的评估与选择 Content 6. 学习模型的评估与选择 6.1 如何调试学习算法 6.2 评估假设函数(Evaluating a hypothesis) 6.3 模型选择与训练/验证/ ...
- 洛谷U4807抽水机[最小生成树]
题目背景 kkk被Farmer John和他的奶牛贝茜虐的很惨,然后她也想体验下一个Farmer的生活.但她又懒得种地,就选择养鱼. 题目描述 这些鱼都是热带鱼(废话),很娇贵(比kkk娇贵),要经常 ...
- css3属性选择器
- Python的高级特性12:类的继承
在面向对象的程序设计中,继承(Inheritance)允许子类从父类那里获得属性和方法,同时子类可以添加或者重载其父类中的任何方法.在C++和Java的对象模型中,子类的构造函数会自动调用父类的构造函 ...
- sublime快捷键<转>
写在前面的话:平时做项目中在用eclipse和vs,但是对于一些小项目,感觉没有必要搞那么大的一个工具使用,比如写个小微商城,搞个小脚本了什么,所以就一直在用Sublime Text,界面清新简洁,没 ...
- C#输出log信息
在写程序的过程中,有时候我们需要添加一些log信息,这个时候,可以采用下面的方法来实现. public static void WriteLog(string ExtraMsg, Exception ...
- 独立成分分析(ICA)在fMRI数据处理时timecourse的理解
来源: http://blog.sciencenet.cn/blog-479412-434990.html 在处理fMRI数据时,使用空间ICA的方法.将一个四维的fMRI数据分解为空间patte ...
- NOIP2016提高组解题报告
NOIP2016提高组解题报告 更正:NOIP day1 T2天天爱跑步 解题思路见代码. NOIP2016代码整合
- Protocol https not supported or disabled in libcurl
最后用PHP Curl 模拟访问HTTPS ,总是得到 Protocol https not supported or disabled in libcurl 错误,奇怪了,找了很多资料,有人说没有开 ...
- [MetaHook] Find a function signature
Find a non-public function signature, we need a tool "IDA Pro" ( You can open picture in a ...