1
0
Fork 0

Update sa.cc

This commit is contained in:
Ariel 2024-01-20 15:11:37 +08:00 committed by GitHub
parent 031a698ef5
commit d0330f151b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 1 deletions

View File

@ -9,7 +9,7 @@ const int N = 1000010;
char s[N]; char s[N];
// key1[i] = rk[id[i]](作为基数排序的第一关键字数组) // key1[i] = rk[id[i]](作为基数排序的第一关键字数组)
int n, sa[N], rk[N], oldrk[N << 1], id[N], key1[N], cnt[N]; int n, sa[N], rk[N], oldrk[N << 1], id[N], key1[N], cnt[N], height[N];
bool cmp(int x, int y, int w) { bool cmp(int x, int y, int w) {
return oldrk[x] == oldrk[y] && oldrk[x + w] == oldrk[y + w]; return oldrk[x] == oldrk[y] && oldrk[x + w] == oldrk[y + w];
@ -20,6 +20,8 @@ int main() {
scanf("%s", s + 1); scanf("%s", s + 1);
n = strlen(s + 1); n = strlen(s + 1);
// calc sa[]
for (i = 1; i <= n; ++i) ++cnt[rk[i] = s[i]]; for (i = 1; i <= n; ++i) ++cnt[rk[i] = s[i]];
for (i = 1; i <= m; ++i) cnt[i] += cnt[i - 1]; for (i = 1; i <= m; ++i) cnt[i] += cnt[i - 1];
for (i = n; i >= 1; --i) sa[cnt[rk[i]]--] = i; for (i = n; i >= 1; --i) sa[cnt[rk[i]]--] = i;
@ -43,6 +45,14 @@ int main() {
} }
} }
// calc height[]
for (i = 1, k = 0; i <= n; ++i) {
if (rk[i] == 0) continue;
if (k) --k;
while (s[i + k] == s[sa[rk[i] - 1] + k]) ++k;
height[rk[i]] = k;
}
for (i = 1; i <= n; ++i) printf("%d ", sa[i]); for (i = 1; i <= n; ++i) printf("%d ", sa[i]);
return 0; return 0;