Bzoj 1396: Identifying substrings (suffix array + segment tree)

Source: Internet
Author: User

The great God seems to be using the suffix automaton?..... Konjac Konjac on Xiu Show IQ write a suffix array solution .....

After finding the height array, we enumerate each bit as the beginning of the substring.

such as (x, y is the height value), the Heights array in the adjacent 3 suffixes, if we enumerate the first character of S2 to start, then we find that the length of at least len = max (x, y) +1, to meet the test instructions (only once). This is a good brain tonic ... Because S2 and other strings of LCP are RMQ, it will certainly <=LCP (S1,S2) or <=LCP (S2,S3). Then you use Len to update the answer to the first Len characters in S2, and the segment tree is maintained. And then for the length of the Lth>len is certainly legal, they are S2 of the first lth characters have contributed ... But in fact Lth's contribution to the first lth-1 character C is no egg. (because the contribution of a string ending in C that is less than the beginning of the same character, or the contribution of Len), Lth>=len contributes to the lth character.

It is easy to see that such contributions are arithmetic progression .... The line tree maintenance is OK.

Time complexity o (n log n), space complexity O (n)

-------------------------------------------------------------------------

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 100009;Char S[MAXN];int N, L, R, Val;int HEIGHT[MAXN], RANK[MAXN], SA[MAXN], CNT[MAXN]; inline void Min (int &x, int t) {if (t < x) x = t;}inline void Max (int &x, int t) {if (T > x) x = t;}void Buildsa (int m) {int *x = Height, *y = Rank;for (int i = 0; i < m; i++) cnt[i] = 0;for (int i = 0; i < N; i++) cnt[x[i] = s[i]]++;for (int i = 1; i < m; i++) cnt[i] + = cnt[i-1];for (int i = N; i--;) Sa[--cnt[x[i]] = i;for (int k = 1, p = 0; k <= N; k <<= 1, p = 0) {for (int i = n-k; i < N; i++) y[p++] = i;for (int i = 0; i < N; i++)if (Sa[i] >= k) y[p++] = sa[i]-K;for (int i = 0; i < m; i++) cnt[i] = 0;for (int i = 0; i < N; i++) cnt[x[y[i]]]++;for (int i = 1; i < m; i++) cnt[i] + = cnt[i-1];for (int i = N; i--;) Sa[--cnt[x[y[i]] [y[i];swap (x, y);p = (x[sa[0]] = 0) + 1;for (int i = 1; i < N; i++) {if (y[sa[i]! = y[sa[i-1]] | | y[sa[i] + K]! = Y[sa[i-1] + K]) p++;X[sa[i]] = p-1;}if ((M = p) >= N) break;}for (int i = 0; i < N; i++) rank[sa[i]] = i;height[0] = 0;for (int i = 0, h = 0; i < N; i++) if (Rank[i]) {if (h) h--;while (s[i + h] = = S[sa[rank[i]-1] + h]) h++;Height[rank[i]] = h;}}struct Node {Node *LC, *RC;int n, D;inline void pd (int len) {if (n! = maxn) {Min (lc->n, n);Min (rc->n, n);}if (d! = MAXN) {Min (lc->d, d);Min (rc->d, D + ((len + 1) >> 1));}}} POOL[MAXN << 1], *pt = Pool, *root;void Build (node* t, int l, int r) {t->n = T->d = MAXN;if (l! = r) {int m = (L + r) >> 1;Build (T->LC = pt++, L, m);Build (T->RC = pt++, M + 1, R);}}void Modify (node* t, int l, int r) {if (l <= l && R <= R) {Min (T->n, Val);} else {int m = (L + r) >> 1;if (l <= m) Modify (T->LC, L, m);if (M < R) Modify (T->RC, M + 1, R);}}void Change (node* t, int l, int r) {if (l <= l && R <= R) {Min (t->d, Val + l-l);} else {int m = (L + r) >> 1;if (l <= m) Change (T->LC, L, m);if (M < R) Change (T->RC, M + 1, R);}}void DFS (node* t, int l, int r) {if (l! = r) {int m = (L + r) >> 1;T->PD (r-l + 1);DFS (T->LC, L, m);DFS (T->RC, M + 1, R);} elseprintf ("%d\n", Min (t->d, t->n));}int main () {scanf ("%s", s);N = strlen (S);s[n++] = ' $ ';BUILDSA (' z ' + 1);int n = N-1;Build (Root = pt++, 1, n);Height[n] = 0;for (int i = 1; i < N; i++) {Val = max (Height[i], height[i + 1]) + 1;if (Val > 1) {if (Sa[i] + Val > N) continue;L = sa[i] + 1, R = L + Val-2;Modify (Root, 1, n);}L = sa[i] + Val, R = n;if (L > R) continue;Change (Root, 1, n);}DFS (Root, 1, n);return 0;}

-------------------------------------------------------------------------

1396: Identify substring time limit: ten Sec Memory Limit: 162 MB
Submit: 201 Solved: 119
[Submit] [Status] [Discuss] Descriptioninput a line, a lowercase string s, with a length of not more than 10^5outputl lines, an integer per line, and the data in row I represent how long the shortest identifying substring of the element I of S is. Sample InputAgoodcookcooksgoodfood
Sample Output1
2
3
3
2
2
3
3
2
2
3
3
2
1
2
3
3
2
1
2
3
4
HINT

Source

Bzoj 1396: Identifying substrings (suffix array + segment tree)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.