Created
February 4, 2016 09:06
-
-
Save Hikari9/2d11455e869c325b0a7c to your computer and use it in GitHub Desktop.
Hackerrank HourRank round 5 difficult question, incomplete
This file contains hidden or 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
#include <iostream> | |
#include <cstdio> | |
#include <cstring> | |
#include <algorithm> | |
using namespace std; | |
const int N = 100005; | |
int n, q; long long k; | |
char s[N]; int pos[N], sa[N], tmp[N], lcp[N], gap; | |
inline bool cmp(int i, int j) { | |
if (pos[i] != pos[j]) return pos[i] < pos[j]; | |
i += gap, j += gap; | |
return i < n && j < n ? pos[i] < pos[j] : i > j; | |
} | |
void build() { | |
s[n++] = '$'; | |
for (int i = 0; i < n; ++i) { | |
sa[i] = i; | |
pos[i] = s[i]; | |
} | |
for (gap = 1;; gap <<= 1) { | |
sort(sa, sa + n, cmp); | |
for (int i = 1; i < n; ++i) | |
tmp[i] = tmp[i - 1] + cmp(sa[i - 1], sa[i]); | |
for (int i = 0; i < n; ++i) | |
pos[sa[i]] = tmp[i]; | |
if (tmp[n - 1] == n - 1) | |
break; | |
} | |
for (int i = 0, k = 0; i < n; ++i) { | |
if (pos[i] + 1 < n) { | |
for (int j = sa[pos[i] + 1]; s[i + k] == s[j + k]; ++k); | |
lcp[pos[i]] = k; | |
if (k) --k; | |
} else { | |
lcp[pos[i]] = 0; | |
} | |
} | |
for (int i = 1; i < n; ++i) { | |
if (lcp[i]) | |
} | |
} | |
int main() { | |
scanf("%d%d", &n, &q); | |
scanf("%s", s); | |
build(); | |
while (q--) { | |
scanf("%lld", &k); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment