SuffixArray.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Routine for computing the length of the longest common prefix of any two
  2. // suffixes
  3. const int maxn = 1000;
  4. struct SuffixArray {
  5. char s[maxn]; //original string, the last character must be 0 and there is no 0 before.
  6. int sa[maxn]; //sa[i] = the index of the i-th smallest suffix.
  7. int rrank[maxn]; //rank[i] = rank of the suffix of index i. rank[0] must be n-1.
  8. int height[maxn]; // height[i] = The length of the longest common prefix of sa[i-1] and sa[i]
  9. int t[maxn], t2[maxn], c[maxn]; // aux
  10. int n; // the number of character.
  11. void clear() {
  12. n = 0;
  13. memset(sa, 0, sizeof(sa));
  14. memset(t, 0 , sizeof(t));
  15. memset(t2, 0 , sizeof(t2));
  16. }
  17. // m = max(char) + 1;
  18. //Build the suffix array of string s, every character should be between 0 and m-1.
  19. // initialiser s and n before calling this function.
  20. void build_sa(int m) {
  21. int i, *x = t, *y = t2;
  22. for(i = 0; i < m; i++) c[i] = 0;
  23. for(i = 0; i < n; i++) c[x[i] = s[i]]++;
  24. for(i = 1; i < m; i++) c[i] += c[i-1];
  25. for(i = n-1; i >= 0; i--) sa[--c[x[i]]] = i;
  26. for(int k = 1; k <= n; k <<= 1) {
  27. int p = 0;
  28. for(i = n-k; i < n; i++) y[p++] = i;
  29. for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i]-k;
  30. for(i = 0; i < m; i++) c[i] = 0;
  31. for(i = 0; i < n; i++) c[x[y[i]]]++;
  32. for(i = 0; i < m; i++) c[i] += c[i-1];
  33. for(i = n-1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
  34. swap(x, y);
  35. p = 1; x[sa[0]] = 0;
  36. for(i = 1; i < n; i++)
  37. x[sa[i]] = y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+k]==y[sa[i]+k] ? p-1 : p++;
  38. if(p >= n) break;
  39. m = p;
  40. }
  41. }
  42. void build_height() {
  43. int i, k = 0;
  44. for(i = 0; i < n; i++) rrank[sa[i]] = i;
  45. for(i = 0; i < n; i++) {
  46. if(k) k--;
  47. int j = sa[rrank[i]-1];
  48. while(s[i+k] == s[j+k]) k++;
  49. height[rrank[i]] = k;
  50. }
  51. }
  52. //The LCP of any two suffx = RMQ(height[i+1],...,height[j])
  53. };
  54. void test() {
  55. SuffixArray SA;
  56. strcpy(SA.s, "1122330");
  57. SA.clear();
  58. SA.n = strlen(SA.s);
  59. SA.build_sa(100);
  60. SA.build_height();
  61. for (int i = 0; i < SA.n; i++)
  62. cout << i << " " << SA.s[i] << " " << SA.sa[i] << " " << SA.rrank[i] << endl;
  63. }