后缀数组

 

study from :

http://www.cnblogs.com/zwfymqz/p/8413523.html

 

https://www.luogu.org/problemnew/show/P3809

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <iostream>
 6 using namespace std;
 7 const int maxn=1e6+10;
 8 
 9 char str[maxn];
10 int gg,n,fir[maxn],sec[maxn],pos[maxn],ind[maxn];
11 
12 void radixsort()
13 {
14     int i;
15     for (i=0;i<n;i++)
16         ind[i]=0;
17     for (i=1;i<=n;i++)
18         ind[pos[i]]++;
19     for (i=1;i<=gg;i++)///!
20         ind[i]+=ind[i-1];
21     for (i=n;i>=1;i--)
22         fir[ind[pos[sec[i]]]--]=sec[i];
23 }
24 
25 int main()
26 {
27     int g,i,w;
28     scanf("%s",str+1);
29     n=strlen(str+1);
30 
31     gg='z'-'0'+1;
32     for (i=1;i<=n;i++)
33         pos[i]=str[i]-'0'+1,sec[i]=i;
34     radixsort();
35     for (w=1,g=0;g<n;gg=g,w<<=1)
36     {
37         g=0;
38         for (i=1;i<=w;i++)
39             sec[++g]=n-w+i;
40         for (i=1;i<=n;i++)
41             if (fir[i]>w)
42                 sec[++g]=fir[i]-w;
43 
44         radixsort();
45         swap(sec,pos);///address change
46 
47         pos[fir[1]]=g=1;
48         for (i=2;i<=n;i++)
49             ///现在的sec是以前的pos
50             pos[fir[i]]= (sec[fir[i-1]]==sec[fir[i]] && sec[fir[i-1]+w]==sec[fir[i]+w])?g:++g;
51     }
52 
53     for (i=1;i<=n;i++)
54     {
55         if (i!=1)
56             printf(" ");
57         printf("%d",fir[i]);
58     }
59 
60     return 0;
61 }
62 /*
63 sa[i]:
64 排名为i的后缀的位置
65 
66 rak[i]
67 :从第i个位置开始的后缀的排名,下文为了叙述方便,把从第i个位置开始的后缀简称为后缀i
68 
69 tp[i]
70 :基数排序的第二关键字,意义与sa一样,即第二关键字排名为i的后缀的位置
71 
72 tax[i]
73 :i号元素出现了多少次。辅助基数排序
74 
75 s
76 :字符串,s[i]表示字符串中第i个字符串
77 
78 sa fir
79 tp sec
80 rak pos
81 tax ind
82 */

 

study博客的height和一些题需要补

posted @ 2018-12-04 11:39  congmingyige  阅读(179)  评论(0编辑  收藏  举报