http://www.spoj.com/problems/SUBLEX/

好难啊。

建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量。该状态能走到的所有状态的f值的和+1就是当前状态的f值。

最后对于询问的k,从root开始走顺便加加减减就可以了。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. int in() {
  6. int k = 0; char c = getchar();
  7. for(; c < '0' || c > '9'; c = getchar());
  8. for(; c >= '0' && c <= '9'; c = getchar())
  9. k = k * 10 + c - 48;
  10. return k;
  11. }
  12. int tot = 0, par[250003], go[250003][26], val[250003], f[250003], root, last;
  13. void extend(int w) {
  14. int p = last;
  15. int np = ++tot; val[np] = val[p] + 1;
  16. while (p && go[p][w] == 0)
  17. go[p][w] = np, p = par[p];
  18. if (p == 0) par[np] = root;
  19. else {
  20. int q = go[p][w];
  21. if (val[q] == val[p] + 1) par[np] = q;
  22. else {
  23. int nq = ++tot; val[nq] = val[p] + 1;
  24. memcpy(go[nq], go[q], sizeof(go[q]));
  25. par[nq] = par[q];
  26. par[q] = par[np] = nq;
  27. while (p && go[p][w] == q)
  28. go[p][w] = nq, p = par[p];
  29. }
  30. }
  31. last = np;
  32. }
  33. char s[150003];
  34. int len, Q, k, c[150003], id[250003], tmp;
  35. int main() {
  36. root = last = ++tot;
  37. scanf("%s", s + 1);
  38. len = strlen(s + 1);
  39. for(int i = 1; i <= len; ++i)
  40. extend(s[i] - 'a');
  41. for(int i = 1; i <= tot; ++i)
  42. ++c[val[i]];
  43. for(int i = 1; i <= len; ++i)
  44. c[i] += c[i - 1];
  45. for(int i = tot; i >= 1; --i)
  46. id[c[val[i]]--] = i;
  47. int sum, x;
  48. for(int i = tot; i >= 1; --i) {
  49. sum = 0; x = id[i];
  50. for(int j = 0; j < 26; ++j)
  51. sum += f[go[x][j]];
  52. f[x] = sum + 1;
  53. }
  54. Q = in();
  55. while (Q--) {
  56. k = in();
  57. tmp = root;
  58. while (k) {
  59. for(int i = 0; i < 26; ++i)
  60. if (x = go[tmp][i])
  61. if (f[x] >= k) {
  62. putchar('a' + i);
  63. --k;
  64. tmp = x;
  65. break;
  66. } else
  67. k -= f[x];
  68. }
  69. puts("");
  70. }
  71. return 0;
  72. }

【SPOJ 7258】Lexicographical Substring Search的更多相关文章

  1. 【spoj SUBLEX】 Lexicographical Substring Search

    http://www.spoj.com/problems/SUBLEX/ (题目链接) 题意 给出一个字符串,询问其中字典序第K小的子串. Solution 后缀自动机例题. 构出后缀自动机以后,对每 ...

  2. 【SPOJ - SUBLEX】Lexicographical Substring Search 【后缀自动机+dp】

    题意 给出一个字符串和q个询问,每个询问给出一个整数k,输出第k大得子串. 分析 建后缀自动机,利用匹配边来解决.设d[v]为从状态v开始有多少不同的路径.这个显然是可以递推出来的.然后对于每个询问, ...

  3. 【spoj7528】 Lexicographical Substring Search

    http://www.spoj.com/problems/SUBLEX/ (题目链接) 题意 给出一个字符串,询问其中字典序第K小的子串. Solution 后缀自动机例题. 构出后缀自动机以后,对每 ...

  4. SPOJ:SUBLEX - Lexicographical Substring Search

    题面 第一行给定主串\((len<=90000)\) 第二行给定询问个数\(T<=500\) 随后给出\(T\)行\(T\)个询问,每次询问排名第\(k\)小的串,范围在\(int\)内 ...

  5. spoj 7258 Lexicographical Substring Search (后缀自动机)

    spoj 7258 Lexicographical Substring Search (后缀自动机) 题意:给出一个字符串,长度为90000.询问q次,每次回答一个k,求字典序第k小的子串. 解题思路 ...

  6. SPOJ SUBLEX 7258. Lexicographical Substring Search

    看起来像是普通的SAM+dfs...但SPOJ太慢了......倒腾了一个晚上不是WA 就是RE ..... 最后换SA写了...... Lexicographical Substring Searc ...

  7. SPOJ SUBLEX - Lexicographical Substring Search 后缀自动机 / 后缀数组

    SUBLEX - Lexicographical Substring Search Little Daniel loves to play with strings! He always finds ...

  8. 【 SPOJ - GRASSPLA】 Grass Planting (树链剖分+树状数组)

    54  种草约翰有 N 个牧场,编号为 1 到 N.它们之间有 N − 1 条道路,每条道路连接两个牧场.通过这些道路,所有牧场都是连通的.刚开始的时候,所有道路都是光秃秃的,没有青草.约翰会在一些道 ...

  9. [SPOJ7258]Lexicographical Substring Search

    [SPOJ7258]Lexicographical Substring Search 试题描述 Little Daniel loves to play with strings! He always ...

随机推荐

  1. Stanford机器学习笔记-6. 学习模型的评估和选择

    6. 学习模型的评估与选择 Content 6. 学习模型的评估与选择 6.1 如何调试学习算法 6.2 评估假设函数(Evaluating a hypothesis) 6.3 模型选择与训练/验证/ ...

  2. 洛谷U4807抽水机[最小生成树]

    题目背景 kkk被Farmer John和他的奶牛贝茜虐的很惨,然后她也想体验下一个Farmer的生活.但她又懒得种地,就选择养鱼. 题目描述 这些鱼都是热带鱼(废话),很娇贵(比kkk娇贵),要经常 ...

  3. css3属性选择器

  4. Python的高级特性12:类的继承

    在面向对象的程序设计中,继承(Inheritance)允许子类从父类那里获得属性和方法,同时子类可以添加或者重载其父类中的任何方法.在C++和Java的对象模型中,子类的构造函数会自动调用父类的构造函 ...

  5. sublime快捷键<转>

    写在前面的话:平时做项目中在用eclipse和vs,但是对于一些小项目,感觉没有必要搞那么大的一个工具使用,比如写个小微商城,搞个小脚本了什么,所以就一直在用Sublime Text,界面清新简洁,没 ...

  6. C#输出log信息

    在写程序的过程中,有时候我们需要添加一些log信息,这个时候,可以采用下面的方法来实现. public static void WriteLog(string ExtraMsg, Exception ...

  7. 独立成分分析(ICA)在fMRI数据处理时timecourse的理解

    来源: http://blog.sciencenet.cn/blog-479412-434990.html   在处理fMRI数据时,使用空间ICA的方法.将一个四维的fMRI数据分解为空间patte ...

  8. NOIP2016提高组解题报告

    NOIP2016提高组解题报告 更正:NOIP day1 T2天天爱跑步 解题思路见代码. NOIP2016代码整合

  9. Protocol https not supported or disabled in libcurl

    最后用PHP Curl 模拟访问HTTPS ,总是得到 Protocol https not supported or disabled in libcurl 错误,奇怪了,找了很多资料,有人说没有开 ...

  10. [MetaHook] Find a function signature

    Find a non-public function signature, we need a tool "IDA Pro" ( You can open picture in a ...