Guest User

Untitled

a guest
Dec 11th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. std::vector<std::pair<int,int>> suffix_array(const std::vector<std::string>& ss) {
  2. std::vector<std::vector<std::pair<int,int>>> tmp(26);
  3.  
  4. size_t n = 0;
  5. for (size_t i = 0; i < ss.size(); i++) {
  6. if (ss[i].length() > n) {
  7. n = ss[i].length();
  8. }
  9.  
  10. for (size_t j = 0; j < ss[i].length(); j++) {
  11. tmp[ss[i][j] - 'a'].push_back(std::make_pair(i,j));
  12. }
  13. }
  14.  
  15. // initialize pos
  16. std::vector<std::pair<int,int>> pos;
  17. std::vector<bool> bh;
  18. for (auto &v1 : tmp) {
  19. bool b = true;
  20. for (auto &p: v1) {
  21. pos.push_back(p);
  22. bh.push_back(b);
  23. b = false;
  24. }
  25. }
  26.  
  27. // initialze inv_pos
  28. std::map<std::pair<int,int>,int> inv_pos;
  29. for (size_t i = 0; i < pos.size(); i++) {
  30. inv_pos[pos[i]] = i;
  31. }
  32.  
  33. int H = 1;
  34.  
  35. while (H <= n) {
  36. std::vector<int> count(pos.size(), 0);
  37. std::vector<bool> b2h(pos.size(), false);
  38.  
  39. for (size_t i = 0, j = 0; i < pos.size(); i++) {
  40. if (bh[i]) {
  41. j = i;
  42. }
  43. inv_pos[pos[i]] = j;
  44. }
  45.  
  46. int k = 0;
  47. int i = 0;
  48.  
  49. while (i < pos.size()) {
  50. int j = k;
  51. i = j;
  52.  
  53. do {
  54. auto t = std::make_pair(pos[i].first, pos[i].second - H);
  55.  
  56. if (t.second >= 0) {
  57. int q = inv_pos[t];
  58.  
  59. count[q] += 1;
  60. inv_pos[t] += (count[q] - 1);
  61. b2h[inv_pos[t]] = true;
  62. }
  63.  
  64. i++;
  65. } while (i < pos.size() && !bh[i]);
  66.  
  67. k = i;
  68. i = j;
  69.  
  70. do {
  71. auto t = std::make_pair(pos[i].first, pos[i].second - H);
  72.  
  73. if (t.second >= 0) {
  74. int q = inv_pos[t];
  75.  
  76. if ((j <= q) && (q < k) && (j <= (q+1)) &&
  77. ((q+1) < k) && b2h[q+1]) {
  78. b2h[q+1] = false;
  79. }
  80. }
  81.  
  82. i++;
  83. } while (i < k);
  84. }
  85.  
  86. bh = b2h;
  87. for (auto &x : inv_pos) {
  88. pos[x.second] = x.first;
  89. }
  90.  
  91. H *= 2;
  92. }
  93.  
  94. return pos;
  95. }
Add Comment
Please, Sign In to add comment