Hdoj title 3518 boring counting (suffix array, number of sub-strings with a minimum of 2 repetitions)

Source: Internet
Author: User

Boring countingTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 2253 Accepted Submission (s): 924


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
Recommendzhengfeng | We have carefully selected several similar problems for you:3517 3520 3519 3521 3522
problem:3518 (boring counting)     Judge status:acceptedrunid:14564325    language:c++    Author:lwj1994code Render status:rendered by hdoj C + + Code Render Version 0.01 Beta

AC Code

#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream&gt         ;      using namespace Std;     int s[2002];   Char str[2002];      int sa[2002],t1[2002],t2[2002],c[2002];      int Rank[2002],height[2002],ans;          void Build_sa (int s[],int n,int m) {int i,j,p,*x=t1,*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 (j=1;j<=n;j<<=1) {p=0;              for (i=n-j;i<n;i++) y[p++]=i;              for (i=0;i<n;i++) if (sa[i]>=j) y[p++]=sa[i]-j;              for (i=0;i<m;i++) c[i]=0;              for (i=0;i<n;i++) c[x[y[i]]]++;       for (i=1;i<m;i++) c[i]+=c[i-1];       for (i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];              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]+j]==y[sa[i]+j]?p-1:p++;              if (p>=n) break;          M=p;          }} void GetHeight (int s[],int n) {int i,j,k=0;          for (i=0;i<=n;i++) rank[sa[i]]=i;              for (i=0;i<n;i++) {if (k) k--;              J=SA[RANK[I]-1];              while (S[i+k]==s[j+k]) k++;          Height[rank[i]]=k; }}int judge (int n,int len) {int maxn=sa[0],minn=sa[0],ans=0;int i,j;for (i=1;i<=n;i++) {if (Height[i]<len) {if (max N-minn>=len) ans++;maxn=minn=sa[i];} Else{if (Maxn<sa[i]) maxn=sa[i];if (Minn>sa[i]) minn=sa[i];}} if (Maxn-minn>=len) Ans++;return ans;} int main () {int n;while (scanf ("%s", str)!=eof) {int i;if (strcmp (StR, "#") ==0) break;int Len=strlen (str); for (i=0;i<len;i++) s[i]=str[i]-' a ' +1;s[len]=0;build_sa (s,len+1,30); GetHeight (s,len); int l=0,r=len;ans=0;for (i=1;i<=len/2;i++) {Ans+=judge (len,i);} printf ("%d\n", ans);}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdoj title 3518 boring counting (suffix array, number of sub-strings with a minimum of 2 repetitions)

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.