CSU-1632 repeated substrings[suffix array to find the number of substrings that occur repeatedly]

Source: Internet
Author: User

Evaluation Address: https://cn.vjudge.net/problem/CSU-1632

Description

in the string, all occurrences of at least 2 number of sub-strings of the second

Input

first act an integer T (t<=10) represents the number of use case groups, with each group of use cases occupying a length of 100000 the string

Output

For each set of use cases, output the number of substrings that appear at least two times in the string

Sample Input

3

Aabaab

Aaaaa

AaAaA

Sample Output

5

4

5

Solution

Ans=sum ( max (height (i)-height (i-1), 0) )

#include <cstdio>#include<cstring>using namespacestd;Const intn=1e5+5;intT,n,ans,c[n],sa[n],tsa[n],trank[n],rank[n],h[n];CharS[n];voidDA (intmaxx= the) {memset (c,0,sizeofc);intp;  for(intI=1; i<=n;i++) c[rank[i]=s[i]]++;  for(intI=2; i<=maxx;i++) c[i]+=c[i-1];  for(inti=n;i;i--) sa[c[rank[i]]--]=i; trank[sa[1]]=p=1;  for(intI=2; i<=n;i++){        if(rank[sa[i]]!=rank[sa[i-1]]) p++; Trank[sa[i]]=p; }     for(intI=1; i<=n;i++) rank[i]=Trank[i];  for(intk=1;p <n;k<<=1, maxx=p) {P=0;  for(inti=n-k+1; i<=n;i++) tsa[++p]=i;  for(intI=1; i<=n;i++)if(sa[i]>k) tsa[++p]=sa[i]-K; Memset (c,0,sizeofc);  for(intI=1; i<=n;i++) trank[i]=Rank[tsa[i]];  for(intI=1; i<=n;i++) c[trank[i]]++;  for(intI=2; i<=maxx;i++) c[i]+=c[i-1];  for(inti=n;i;i--) sa[c[trank[i]]--]=Tsa[i]; trank[sa[1]]=p=1;  for(intI=2; i<=n;i++){            if(rank[sa[i]]!=rank[sa[i-1]]|| rank[sa[i]+k]!=rank[sa[i-1]+K]) p++; Trank[sa[i]]=p; }         for(intI=1; i<=n;i++) rank[i]=Trank[i]; }     for(intI=1, k=0; i<=n;i++){        intj=sa[rank[i]-1];  while(S[i+k]==s[j+k]) k++; H[rank[i]]=k;if(k>0) k--; }}voidGO () {ans=0;  for(intI=1; i<=n;i++)if(h[i]>h[i-1]) ans+=h[i]-h[i-1]; printf ("%d\n", ans);}intMain () {scanf ("%d",&T);  while(t--) {scanf ("%s", s+1); N=strlen (s+1);        DA ();    GO (); }    return 0;}

CSU-1632 repeated substrings[suffix array to find the number of substrings that occur repeatedly]

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.