HDU 3518 boring counting (suffix array the number of substrings that do not overlap in a string for at least two occurrences)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=3518


Problem Description035 now faced a tough problem,his 中文版 teacher gives him a String,which consists with n lower case l Etter,he must figure out how to many substrings appear at least twice,moreover,such apearances can not overlap all other.
Take the AAAA as an example. " A "apears four times," AA "apears-without overlaping.however,aaa can ' t apear more than one time without overlapin G.since we can get "AAA" from [0-2] (the position of string begins with 0) and [1-3]. But the interval [0-2] and [1-3] overlaps each other. So "AAA" can does not take into account. Therefore,the answer is 2 ("a", and "AA").
Inputthe input data consist with several test cases. The input ends with a line "#". Each test case contain a string consists with lower letter,the length n won ' t exceed (n <= 1000). Outputfor each test case output a integer Ans,which represent the answer for the test case.you ' d better use Int64 to Avoi D unnecessary trouble. Sample Input
aaaaababcabbaaaaaa#
Sample Output
233
Source2010 acm-icpc multi-university Training Contest (9)--host by Hnu


Test instructions

Title, to find the number of substrings that do not overlap in a string at least two times!

Ps:

Using the properties of the suffix array,

First, the suffix array of SA, rank, height array to find out!

Then in enumerating the length of the substring I, and then in succession (as to why continuous will be explained below) and the value is greater than the length of the enumeration of the height array to find the maximum and minimum values,

If the difference between the maximum and minimum values is greater than or equal to the enumerated length I, then the answer increases by one;

Why do you need a continuous height array?

Because this can no longer consider the problem of repeated counting, if there is the same prefix then their rank ranking must be adjacent, then the height is continuous, the prefix is different to do another judgment!

The code is as follows:

#include <cstdio> #include <cstring>const int N = 1017;int Wa[n], wb[n], wv[n], Ws[n];int rank[n]; Rank array int height[n]; Rank the longest common prefix of the adjacent two suffixes Char str[n];int s[n], sa[n]; The SA is a suffix array, and n suffixes are sorted from small to large to the beginning of the ordered suffix int Max (int a, int b) {return a > B? a:b;} int Min (int a, int b) {return a < b? A:b;} int cmp (int *r, int A, int b, int l) {return r[a]==r[b] && r[a+l]==r[b+l];} The parameter n of the GET_SA function represents the number of characters in the string, where n is the parameter m of the 0//get_sa function that is added to the end of the string, which is the value range of the characters in the string, and is a parameter of the radix sort, or if the original sequence is a letter can be directly taken 128,/ /If the original sequence itself is an integer, M can take a value 1 larger than the largest integer.    void Get_sa (int *r, int *sa, int n, int m)//multiplication algorithm {int i,j,p,*x=wa,*y=wb,*t;    for (i=0; i<m; i++) ws[i]=0;    for (i=0; i<n; i++) ws[x[i]=r[i]]++;    for (I=1; i<m; i++) ws[i]+=ws[i-1]; for (i=n-1; i>=0; i--) sa[--ws[x[i]]]=i;        Sorts a string of length 1 for (p=1,j=1; p<n; j*=2,m=p) {for (p=0,i=n-j; i<n; i++) y[p++]=i; for (i=0; i<n; i++) if (sa[i]>=j) y[p++]=sa[i]-j;   The second keyword sort results for (i=0; i<n; i++) wv[i]=x[y[i];     for (i=0; i<m; i++) ws[i]=0;        for (i=0; i<n; i++) ws[wv[i]]++;        for (I=1; i<m; i++) ws[i]+=ws[i-1]; for (i=n-1; i>=0; i--) sa[--ws[wv[i]]]=y[i]; First keyword sort for (t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++) x[sa[i]]=cmp (y,sa[i-1],sa[i],j)? p-1:p++; Update rank array} return;    void Get_height (int *r, int *sa, int n)//height array {int I, j, k=0;    for (I=1; 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++); return;}        int main () {while (scanf ("%s", str)! = EOF) {int len = strlen (str);        if (str[0] = = ' # ') break;        for (int i = 0; i < len; i++) s[i] = str[i]-' a ' +1;        S[len] = 0;//This assignment is 0 is the key Get_sa (s,sa,len+1,27);        Get_height (S,sa,len);        for (int i=0;i<len+1;i++) printf ("%d%d\n", i,sa[i]);        int ans = 0; for (int i = 1; I <= (len+1)/2; i++) {//Check half is good, the length is greater than (len+1)/The substring of 2 cannot be repeated twice//length of I repeat substring int minn = N;            int MAXN =-1;                    for (int j = 1; j <= Len; j + +) {if (Height[j] >= i)//continuous {                    Minn = Min (Minn,min (Sa[j-1],sa[j]));                MAXN = Max (Maxn,max (sa[j-1],sa[j));                        } else//If the value of height is not consecutive {if (minn+i <= MAXN) greater than I                    ans++;                Minn = N, MAXN =-1;        }} if (Minn+i <= maxn) ans++;    } printf ("%d\n", ans); } return 0;}


Finally, two more links to explain the suffix array:

1, Http://wenku.baidu.com/link?url=g6BzfQTbVcypv0wSYhjw0dtlf6eKSp20Jm5KZDT_EzMKZec_ Tngncuu7lzuwcyc6bvhc73rag2e0uda2lmyktkmgeuhawkxjv6ca8k3zzg_


2,http://www.cnblogs.com/staginner/archive/2012/02/02/2335600.html


HDU 3518 boring counting (suffix array the number of substrings that do not overlap in a string for at least two occurrences)

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.