Last active
June 13, 2021 03:09
-
-
Save pervognsen/7113ccc1f4c00c6eb08d529097e0e0ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define MAXN 65536 | |
#define MAXLG 17 | |
char A[MAXN]; | |
struct entry { | |
int nr[2], p; | |
} L[MAXN]; | |
int P[MAXLG][MAXN], N, i, stp, cnt; | |
int cmp(struct entry a, struct entry b) | |
{ | |
return a.nr[0] == b.nr[0] ? (a.nr[1] < b.nr[1] ? 1 : 0) : (a.nr[0] < b.nr[0] ? 1 : 0); | |
} | |
int main() | |
{ | |
gets(A); | |
for (N = strlen(A), i = 0; i < N; i ++) | |
P[0][i] = A[i] - 'a'; | |
// O(log n) doubling steps, so O(sort(n) * log n) total time: O(n log n) with radix sort, otherwise O(n (log n)^2). | |
for (stp = 1, cnt = 1; cnt >> 1 < N; stp ++, cnt <<= 1) | |
{ | |
for (i = 0; i < N; i ++) | |
{ | |
L[i].nr[0] = P[stp - 1][i]; | |
L[i].nr[1] = i + cnt < N ? P[stp - 1][i + cnt] : -1; | |
L[i].p = i; | |
} | |
std::sort(L, L + N, cmp); // O(n log n) comparison sort. Can replace with O(n) radix sort on (nr[0], nr[1]). | |
for (i = 0; i < N; i ++) | |
P[stp][L[i].p] = i > 0 && L[i].nr[0] == L[i - 1].nr[0] && L[i].nr[1] == L[i - 1].nr[1] ? P[stp][L[i - 1].p] : i; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment