后缀数组

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr
 13 #include <string>
 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 23 #define fo(a,b,c) for(register int a=b;a<=c;++a)
 24 #define fr(a,b,c) for(register int a=b;a>=c;--a)
 25 #define mem(a,b) memset(a,b,sizeof(a))
 26 #define pr printf
 27 #define sc scanf
 28 #define ls rt<<1
 29 #define rs rt<<1|1
 30 typedef long long ll;
 31 #define rint register int
 32 void swapp(int &a,int &b);
 33 double fabss(double a);
 34 int maxx(int a,int b);
 35 int minn(int a,int b);
 36 int Del_bit_1(int n);
 37 int lowbit(int n);
 38 int abss(int a);
 39 //const long long INF=(1LL<<60);
 40 const double E=2.718281828;
 41 const double PI=acos(-1.0);
 42 const int inf=(1<<30);
 43 const double ESP=1e-9;
 44 const int mod=(int)1e9+7;
 45 const int N=(int)2e6+10;
 46 
 47 char s[N];
 48 int a[N];
 49 int sa[N],x[N],y[N],t[N];//桶的大小要为N,因为放的是rank;
 50 void Init(int _[],int n)
 51 {
 52     for(rint i=0;i<=n;++i)
 53         _[i]=0;
 54 }
 55 bool cmp(int i,int j,int k)
 56 {
 57     return y[i]==y[j]&&y[i+k]==y[j+k];
 58 }
 59 void Sort(int len)
 60 {
 61     int m=150;//字符集大小;
 62     for(rint i=1;i<=len;++i) ++t[a[i]],x[i]=a[i];
 63     for(rint i=1;i<=m;++i) t[i]+=t[i-1];
 64     for(rint i=len;i>=1;--i) sa[t[x[i]]--]=i;
 65     for(rint k=1;k<=len;k<<=1)
 66     {
 67         int cnt=0;
 68         //按第二个rank排;
 69         for(rint i=len-k+1;i<=len;++i) y[++cnt]=i;
 70         for(rint i=1;i<=len;++i)if(sa[i]>k) y[++cnt]=sa[i]-k;
 71         //按第一个rank排;
 72         Init(t,m);
 73         for(rint i=1;i<=len;++i) ++t[x[y[i]]];
 74         for(rint i=1;i<=m;++i) t[i]+=t[i-1];
 75         for(rint i=len;i>=1;--i) sa[t[x[y[i]]]--]=y[i];
 76         
 77         swap(x,y);x[sa[1]]=cnt=1;
 78         for(rint i=2;i<=len;++i)
 79             x[sa[i]]=cmp(sa[i],sa[i-1],k)?cnt:++cnt;
 80         if(cnt==len)break;
 81         m=cnt;
 82     }
 83 }
 84 
 85 int main()
 86 {
 87 //    freopen("D:\\Chrome Download\\testdata (2).in","r",stdin);
 88     sc("%s",s+1);
 89     int len=strlen(s+1);
 90     for(int i=1;i<=len;++i)
 91         a[i]=s[i];
 92     Sort(len);
 93     for(rint i=1;i<=len;++i)
 94         pr("%d%c",sa[i],i==len?'\n':' ');
 95     return 0;
 96 }
 97 
 98 /**************************************************************************************/
 99 
100 int maxx(int a,int b)
101 {
102     return a>b?a:b;
103 }
104 
105 void swapp(int &a,int &b)
106 {
107     a^=b^=a^=b;
108 }
109 
110 int lowbit(int n)
111 {
112     return n&(-n);
113 }
114 
115 int Del_bit_1(int n)
116 {
117     return n&(n-1);
118 }
119 
120 int abss(int a)
121 {
122     return a>0?a:-a;
123 }
124 
125 double fabss(double a)
126 {
127     return a>0?a:-a;
128 }
129 
130 int minn(int a,int b)
131 {
132     return a<b?a:b;
133 }

 

posted @ 2019-09-12 19:11  ZMWLxh  阅读(184)  评论(0编辑  收藏  举报