fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef int ll;
  4. const ll mx=200001;
  5. ll sa[mx],lcp[mx],pos[mx],tmp[mx];
  6. string a;
  7. ll d=1,n;
  8. bool cmp(ll x,ll y){
  9. if(pos[x]!=pos[y]) return pos[x]<pos[y];
  10. x+=d;y+=d;
  11. return x<n&&y<n?pos[x]<pos[y]:x>y;
  12. }
  13. void findSA(){
  14. for(ll i=0;i<n;++i){
  15. sa[i]=i;
  16. pos[i]=a[i];
  17. }
  18. for(d=1;;d<<=1){
  19. sort(sa,sa+n,cmp);
  20. tmp[0]=0;
  21. for(ll i=1;i<n;++i) tmp[i]=tmp[i-1]+cmp(sa[i-1],sa[i]);
  22. for(ll i=0;i<n;++i) pos[sa[i]]=tmp[i];
  23.  
  24. for(int i =0;i<n;++i){
  25. cout<<tmp[i]<<" "<<a.substr(sa[i])<<endl;
  26. }
  27. cout<<a<<endl;
  28. for(int i =0;i<n;++i) cout<<pos[i];cout<<endl;
  29. for(int i =0;i<n;++i) cout<<sa[i];cout<<endl;
  30. if(/*tmp[n-1]==n-1 ||*/ d>=n) break;
  31. }
  32. }
  33. int main() {
  34. a = "BANANANA";
  35. n = a.size();
  36. cout<<a<<endl;
  37. findSA();
  38. // your code goes here
  39. return 0;
  40. }
Success #stdin #stdout 0.01s 5600KB
stdin
hippopotomonstrosesquippedaliophobia 
stdout
BANANANA
0 A
1 ANANANA
1 ANANA
1 ANA
2 BANANANA
3 NANANA
3 NANA
3 NA
BANANANA
21313130
71350246
0 A
1 ANA
2 ANANANA
2 ANANA
3 BANANANA
4 NA
5 NANANA
5 NANA
BANANANA
32525140
75130624
0 A
1 ANA
2 ANANA
3 ANANANA
4 BANANANA
5 NA
6 NANA
7 NANANA
BANANANA
43726150
75310642
0 A
1 ANA
2 ANANA
3 ANANANA
4 BANANANA
5 NA
6 NANA
7 NANANA
BANANANA
43726150
75310642