Suffix array spoj 694 Distinct substrings

Source: Internet
Author: User

Topic links

Test instructions: Given a string, find the number of different substrings

Analysis: We can know the length of the same prefix between suffixes, if all suffixes follow suffix (sa[0]), suffix (sa[1]), suffix (sa[2]), suffix (sa[n]) in the order of calculation, it is not difficult to find, For each new add-in suffix suffix (sa[k]), it will produce a new prefix of n-sa[k]+1. But there are height[k] and the prefix of the preceding string is the same. So suffix (sa[k]) will "contribute" out of n-sa[k]+1-height[k] a different substring. The answer to the original question is the summation.

This "contribution" does not overlap, because each time the addition is for the current is not the same substring.

#include <cstdio> #include <cstring> #include <algorithm>const int N = 1e3 + 5;int sa[n], rank[n], height[    N];int T[n], t2[n], C[n];char s[n];void da (char *s, int N, int m = n) {int i, p, *x = t, *y = t2;    for (i=0; i<m; ++i) c[i] = 0;    for (i=0; i<n; ++i) c[x[i]=s[i]]++;    for (I=1; i<m; ++i) c[i] + = c[i-1];    for (i=n-1; i>=0;-I.) sa[--c[x[i]] = i;        for (int. k=1; k<=n; k<<=1) {for (p=0, i=n-k; i<n; ++i) y[p++] = i;        for (i=0; i<n; ++i) if (Sa[i] >= k) y[p++] = sa[i]-K;        for (i=0; i<m; ++i) c[i] = 0;        for (i=0; i<n; ++i) c[x[y[i]]]++;        for (i=0; i<m; ++i) c[i] + = c[i-1];        for (i=n-1; i>=0;-i) sa[--c[x[y[i]] [] = y[i];        Std::swap (x, y); p = 1;        X[sa[0]] = 0;        for (I=1; i<n; ++i) {X[sa[i]] = (Y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+k]==y[sa[i]+k]? p-1: p++);        } if (P >= N) break;    m = p; }}void calc_height (intn) {int I, k = 0;    for (i=0; i<n; ++i) rank[sa[i]] = i;        for (i=0; i<n; ++i) {if (k) k--;        Int j = Sa[rank[i]-1];        while (s[i+k] = = S[j+k]) k++;    Height[rank[i]] = k;    }}int N;int Main () {int T; scanf ("%d", &t);        while (t--) {scanf ("%s", s);        n = strlen (s);        n++;        Da (S, N);        Calc_height (n);        int ans = 0;        for (int i=0; i<n; ++i) {ans + n-sa[i]-1-height[i];    } printf ("%d\n", ans); } return 0;}

  

Suffix array spoj 694 Distinct substrings

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.