SPOJ 7258 SUBLEX 后缀自动机
求第k大子串。
按拓扑序处理出一个点往后有多少条路径到终态。
答案就很明显了。
#include <cstring>
#include <cstdio>
#define FOR(i,j,k) for(i=j;i<=k;++i)
const int rt = 1, N = 500005;
int last = 1, cnt = 1, len = 0;
int trans[N][26], fa[N], ma[N], v[N], b[N], l[N], bucket[N];
char str[N];
void add(char c) {int np = ++cnt, p = last, q, nq; last = np; ma[np] = ++len;while (p && !trans[p][c]) trans[p][c] = np, p = fa[p];if (!p) fa[np] = rt;else {q = trans[p][c];if (ma[q] == ma[p] + 1) fa[np] = q;else {nq = ++cnt; memcpy(trans[nq], trans[q], sizeof trans[q]);ma[nq] = ma[p] + 1; fa[nq] = fa[q]; fa[q] = fa[np] = nq;while (p && trans[p][c] == q) trans[p][c] = nq, p = fa[p];}}
}void sort(int *x, int *y, int n, int m) {int i;FOR(i,1,n) ++bucket[x[i]];FOR(i,1,m) bucket[i] += bucket[i - 1];for(i=n;i;--i) y[bucket[x[i]]--] = i;
}int main() {int i, j, n, q, o, k, p;scanf("%s", str + 1);n = strlen(str + 1);FOR(i,1,n) add(str[i] - 'a');sort(ma, b, cnt, n);FOR(i,1,cnt) v[i] = 1;for (i = cnt; i; --i) FOR(j,0,25)if (trans[b[i]][j]) v[b[i]] += v[trans[b[i]][j]];for (scanf("%d", &o); o--; putchar('\n')) {scanf("%d", &k); p = rt;while (k) FOR(i,0,25) if (trans[p][i]) {q = trans[p][i];if (k > v[q]) k -= v[q];else {putchar(i + 'a');p = trans[p][i];--k;break;}}}return 0;
}
SUBLEX - 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”.
Input
In the first line there is Kinan’s string S (with length no more than 90000 characters). It contains only small letters of English alphabet. The second line contains a single integer Q (Q <= 500) , the number of questions Daniel will be asked. In the next Q lines a single integer K is given (0 < K < 2^31).
Output
Output consists of Q lines, the i-th contains a string which is the answer to the i-th asked question.
Example
Input:
aaa
2
2
3
Output:
aa
aaa
SPOJ 7258 SUBLEX 后缀自动机相关推荐
- SPOJ 7258 SUBLEX 后缀数组_二分答案_前缀和
SPOJ 7258 SUBLEX 后缀数组_二分答案_前缀和 Code: #include <cstdio> #include <algorithm> #include < ...
- SPOJ - NSUBSTR Substrings(后缀自动机)
题目链接:点击查看 题目大意:给出一个字符串,求出每个长度下的子串出现最多的次数 题目分析:对原串跑后缀自动机然后记录每个节点的right集合的大小就是当前节点出现的次数了,这个出现次数显然可以从子节 ...
- spoj 1811 LCS 后缀自动机
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- SPOJ7258 SUBLEX 后缀自动机
题目链接 链接是洛谷有翻译的链接. 题意: 给你一个字符串,有T次询问,每次问你在整个字符串中排名为k的子串是哪一个.字符串长度<=90000,T<=500,只统计本质不同的串. 题解: ...
- SPOJ 7258 SUBLEX (SAM)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题目:给出一个串,查询字典序排在第k个的是哪个子串 ...
- SPOJ 1811 LCS [后缀自动机]
题意: 求两个串的最大连续子串 一个串建SAM,另一个串在上面跑 注意如果走了Suffix Link,sum需要更新为t[u].val+1 Suffix Link有点像失配吧,当前状态s走不了了就到S ...
- spoj 8222 Substrings (后缀自动机)
spoj 8222 Substrings 题意:给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值.求F(1)..F(Length(S)) 解题思路:我们构造S的SAM,那么对于 ...
- 【SPOJ】7258. Lexicographical Substring Search(后缀自动机)
http://www.spoj.com/problems/SUBLEX/ 后缀自动机系列完成QAQ...撒花..明天or今晚写个小结? 首先得知道:后缀自动机中,root出发到任意一个状态的路径对应一 ...
- SPOJ 7258 (后缀自动机)
转载:http://hzwer.com/4492.html 给一个长度不超过90000的串S,每次询问它的所有不同子串中,字典序第K小的,询问不超过500个. 搞出后缀自动机 dp处理出每个点往下走能 ...
最新文章
- 【每日一算法】 合并两个有序链表
- MDK 的编译过程及文件类型全解
- 【编译原理】构建一个简单的解释器(Let’s Build A Simple Interpreter. Part 8.)(笔记)一元运算符正负(+,-)
- 把pcl的VTK显示融合到MFC(代码找原作者)
- 使用Apache Kafka,Kubernetes和Envoy,Istio,Linkerd的服务网格和云原生微服务
- 使用CLI设置WildFly绑定地址并关闭
- 《Python自动化》学习笔记:百度云智能实现提取身份证信息
- BM25算法的python实现
- 使用Cscope阅读Linux源码
- mybitis SQL insert into 多条数据
- 计算机财务管理中表格的应用,excel在财务管理中的应用学习心得
- android放大镜无广告,放大镜微件 | Android 开发者 | Android Developers
- excel填充序列_EXCEL如何填充字母序列
- 如何解决电脑桌面图标显示为空白文件?
- 树莓派正式开售CM4以及CM4 Lite,32个不同配置,最低25美元起售!
- SVM中对偶、凸优化与KTT条件问题
- 从《欲望都市》到《绝望主妇》 美剧的疯狂
- 使用Python操作Excel图表之 为最后一个数据点添加数据标签
- 【Caffe】Windows下caffe安装详解
- 华为OJ——删除字符串中出现次数最少的