HDOJ 题目3518 Boring counting(后缀数组,求不重叠反复次数最少为2的子串种类数)

时间:2022-11-16 14:52:59

Boring counting

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2253    Accepted Submission(s): 924

Problem Description
035 now faced a tough problem,his english teacher gives him a string,which consists with n lower case letter,he must figure out how many substrings appear at least twice,moreover,such apearances can not overlap each other.

Take aaaa as an example.”a” apears four times,”aa” apears two times without overlaping.however,aaa can’t apear more than one time without overlaping.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 not take into account.Therefore,the answer is 2(“a”,and “aa”).
 
Input
The 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 1000(n <= 1000).
 
Output
For each test case output an integer ans,which represent the answer for the test case.you’d better use int64 to avoid unnecessary trouble.
 
Sample Input
aaaa
ababcabb
aaaaaa
#
 
Sample Output
2
3
3
 
Source
 
Recommend
zhengfeng   |   We have carefully selected several similar problems for you:  3517 

pid=3520" style="color:rgb(26,92,200); text-decoration:none">3520

 3519 3521 3522 
 

Problem : 3518 ( Boring counting )     Judge Status : Accepted
RunId : 14564325 Language : C++ Author : lwj1994
Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta

HDOJ 题目3518 Boring counting(后缀数组,求不重叠反复次数最少为2的子串种类数)HDOJ 题目3518 Boring counting(后缀数组,求不重叠反复次数最少为2的子串种类数)

ac代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
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(maxn-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);
}
}