fork download
  1. #include <cstdio>
  2. #include <string>
  3. #include <algorithm>
  4.  
  5. const int N = int(3e5) + 2;
  6.  
  7. int n, m, k;
  8. int q[N];
  9. char c[N];
  10.  
  11. int p[N], p_[N];
  12. int e[N], e_[N];
  13. int cnt[N];
  14. int next[N], next_[N];
  15. std::string ans;
  16.  
  17. int main()
  18. {
  19. freopen("input.txt", "rt", stdin);
  20. freopen("output.txt", "wt", stdout);
  21.  
  22. scanf("%d%d%d", &n, &m, &k);
  23.  
  24. for (int i = 0; i < n; i++)
  25. {
  26. scanf("%d %c", q + i, c + i);
  27.  
  28. next[i] = --q[i];
  29. }
  30.  
  31. for (int i = 0; i < n; i++)
  32. cnt[c[i]]++;
  33.  
  34. for (int i = 1; i < 256; i++)
  35. cnt[i] += cnt[i - 1];
  36.  
  37. for (int i = 0; i < n; i++)
  38. p[--cnt[c[i]]] = i;
  39.  
  40. for (int i = 1; i < n; i++)
  41. {
  42. e[p[i]] = e[p[i - 1]];
  43.  
  44. if (c[p[i]] != c[p[i - 1]])
  45. e[p[i]]++;
  46. }
  47.  
  48. for (int w = 0; w < 20; w++)
  49. {
  50. std::fill(cnt, cnt + n, 0);
  51.  
  52. for (int i = 0; i < n; i++)
  53. cnt[e[next[i]]]++;
  54.  
  55. for (int i = 1; i < n; i++)
  56. cnt[i] += cnt[i - 1];
  57.  
  58. for (int i = 0; i < n; i++)
  59. p_[--cnt[e[next[i]]]] = i;
  60.  
  61. std::reverse(p_, p_ + n);
  62.  
  63. std::fill(cnt, cnt + n, 0);
  64.  
  65. for (int i = 0; i < n; i++)
  66. cnt[e[p_[i]]]++;
  67.  
  68. for (int i = 1; i < n; i++)
  69. cnt[i] += cnt[i - 1];
  70.  
  71. for (int i = 0; i < n; i++)
  72. p[--cnt[e[p_[i]]]] = p_[i];
  73.  
  74. for (int i = 0; i < n; i++)
  75. next_[i] = next[i], e_[i] = e[i];
  76.  
  77. e[p[0]] = 0;
  78.  
  79. for (int i = 1; i < n; i++)
  80. {
  81. e[p[i]] = e[p[i - 1]];
  82.  
  83. if (e_[p[i]] != e_[p[i - 1]] ||
  84. e_[next[p[i]]] != e_[next[p[i - 1]]])
  85. e[p[i]]++;
  86. }
  87.  
  88. for (int i = 0; i < n; i++)
  89. next[i] = next_[next_[i]];
  90. }
  91.  
  92. int it = p[k - 1];
  93.  
  94. for (int i = 0; i < m; i++)
  95. {
  96. ans.push_back(c[it]);
  97.  
  98. it = q[it];
  99. }
  100.  
  101. printf("%s", ans.c_str());
  102.  
  103. return 0;
  104. }
Success #stdin #stdout 0s 4352KB
stdin
Standard input is empty
stdout
Standard output is empty