Spoj694 -- suffix Array

Source: Internet
Author: User

Address: http://www.spoj.com/problems/DISUBSTR/

Question meaning:

Here is a string to remove the number of different substrings.

Solution:

Each substring must be the prefix of a suffix, so the original problem is equivalent to finding out the inconsistency between all suffixes.
The number of prefixes. If all suffixes follow suffix (SA [1]), suffix (SA [2]),
Suffix (SA [3]),... , Suffix (SA [N]), it is not difficult to find that
Suffix (SA [k]), which generates N-sa [k] + 1 new prefix. However
Height [k] is the same as the prefix of the preceding string. So suffix (SA [k]) will "Contribute"
Returns N-sa [k] + 1-height [k] Different substrings. The accumulated answer is the answer to the original question. This practice
The time complexity is O (n ).

Code:

# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int maxn = 1000 + 10; char STR [maxn]; int wa [maxn], WB [maxn], WV [maxn]; int C [maxn]; int CMP (int * r, int A, int B, int L) {return R [a] = R [B] & R [A + L] = R [B + L];} void cal_the_houzhui (char * r, int * Sa, int N, int m) {int I, j, P; int * x = wa, * Y = WB; // sort the first character of each Suffix in the base order for (I = 0; I <m; I ++) C [I] = 0; for (I = 0; I <n; I ++) C [x [I] = R [I] ++; for (I = 1; I <m; I + +) C [I] + = C [I-1]; for (I = n-1; I> = 0; I --) sa [-- C [x [I] = I; // then sort the log-level base to make the SA array stable p = 1; for (j = 1; P <n; j * = 2, M = P) {// base sorting for the second dimension // you can use the SA array directly to sort the result. // y [p] indicates that the result is in the two-dimensional sorting, where is the first position of the smallest p? For (P = 0, I = N-J; I <n; I ++) y [p ++] = I; // from N-J ~ N-1 is impossible to have the second-dimensional, because the second-dimensional has exceeded n-1 //, so they are the smallest in the second-dimensional sorting, of course, it is the first for (I = 0; I <n; I ++) if (SA [I]> = J) Y [p ++] = sa [I]-J; for (I = 0; I <n; I ++) // re-define WV [I] = x [Y [I] According to the Y array; for (I = 0; I <m; I ++) c [I] = 0; for (I = 0; I <n; I ++) C [wv [I] ++; for (I = 1; I <m; I ++) C [I] + = C [I-1]; for (I = n-1; I> = 0; I --) sa [-- C [wv [I] = Y [I]; swap (x, y); P = 1; X [SA [0] = 0; for (I = 1; I <n; I ++) x [SA [I] = CMP (Y, sa [I-1], sa [I], j )? P-1: P ++ ;}} int rank [maxn], height [maxn]; void cal_the_height (char * r, int * Sa, int N) {int I, j, k = 0; // obtain rank for (I = 0; I <n; I ++) rank [SA [I] = I; for (I = 0; I <n; height [rank [I ++] = k) {for (K? K --: 0, j = sa [rank [I]-1]; R [I + k] = R [J + k];) K ++ ;}} int N; int _ SA [maxn]; void solve () {cal_the_houzhui (STR, _ SA, N + 1,256); cal_the_height (STR, _ SA, n + 1 ); long long ans = 0; For (INT I = 1; I <= N; I ++) {ans + = N-_ SA [I]-height [I];} printf ("% LLD \ n", ANS) ;}int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% s", STR); n = strlen (STR); solve ();} return 0 ;}

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.