Rkhamim

Untitled

Apr 28th, 2021
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int p[2000008],c[2000007],cnt[2000008],lcp[2000008],ran[2000008],pn[2000007],cn[2000008];
  4. void sort_cyclic_shifts(string const& s) {
  5. int n = s.size();
  6. const int alphabet = 256;
  7. for (int i = 0; i < n; i++)
  8. cnt[s[i]]++;
  9. for (int i = 1; i < alphabet; i++)
  10. cnt[i] += cnt[i-1];
  11. for (int i = 0; i < n; i++)
  12. p[--cnt[s[i]]] = i;
  13. c[p[0]] = 0;
  14. int classes = 1;
  15. for (int i = 1; i < n; i++) {
  16. if (s[p[i]] != s[p[i-1]])
  17. classes++;
  18. c[p[i]] = classes - 1;
  19. }
  20. for (int h = 0; (1 << h) < n; ++h) {
  21. for (int i = 0; i < n; i++) {
  22. pn[i] = p[i] - (1 << h);
  23. if (pn[i] < 0)
  24. pn[i] += n;
  25. }
  26. fill(cnt, cnt + classes, 0);
  27. for (int i = 0; i < n; i++)
  28. cnt[c[pn[i]]]++;
  29. for (int i = 1; i < classes; i++)
  30. cnt[i] += cnt[i-1];
  31. for (int i = n-1; i >= 0; i--)
  32. p[--cnt[c[pn[i]]]] = pn[i];
  33. cn[p[0]] = 0;
  34. classes = 1;
  35. for (int i = 1; i < n; i++) {
  36. pair<int, int> cur = {c[p[i]], c[(p[i] + (1 << h)) % n]};
  37. pair<int, int> prev = {c[p[i-1]], c[(p[i-1] + (1 << h)) % n]};
  38. if (cur != prev)
  39. ++classes;
  40. cn[p[i]] = classes - 1;
  41. }
  42. for (int i = 0; i < n; i++)c[i]=cn[i];
  43. }
  44. }
  45. void suffix_array_construction(string s) {
  46. s += "$";
  47. sort_cyclic_shifts(s);
  48. }
  49. //longest common prefix of string suffix
  50. void lcp_construction(string const& s) {
  51. int n = s.size();
  52. for (int i = 0; i < n; i++)
  53. ran[p[i]] = i;
  54. int k = 0;
  55. for (int i = 0; i < n; i++) {
  56. if (ran[i] == n - 1) {
  57. k = 0;
  58. continue;
  59. }
  60. int j = p[ran[i] + 1];
  61. while (i + k < n && j + k < n && s[i+k] == s[j+k])
  62. k++;
  63. lcp[ran[i]] = k;
  64. if (k)
  65. k--;
  66. }
  67. }
  68. int main()
  69. {
  70. ios_base::sync_with_stdio(false);
  71. cin.tie(NULL);
  72. int i,j,k,t,x,y,z;
  73. string s,s1;
  74. cin>>s>>s1;
  75. z=s.size();
  76. s=s+'#'+s1;
  77. suffix_array_construction(s);
  78. lcp_construction(s);
  79. x=0;
  80. for(i=0;i<s.size()-1;i++)
  81. {
  82. if((p[i]<z&&p[i+1]>z)||(p[i]>z&&p[i+1]<z))
  83. x=max(x,lcp[i]);
  84. }
  85. cout<<x<<endl;
  86.  
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment