Boring counting
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1417 Accepted Submission(s): 581
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”).
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
给你一个字符串,让你求至少出现两次且不重叠的子串的个数。
分别求出后缀数组的sa,rank和height数组,然后枚举子串的长度,记录每组中最小和最大后缀数组的起始位置minn和maxx,并且判断maxx-minn>=k,如果是那么结果+1.
//62MS 284K
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define M 500007
#define inf 0x3f3f3f
using namespace std;
int sa[M],rank[M],height[M];
int wa[M],wb[M],wv[M],ws[M];
char str[1007];
int num[1007];
int cmp(int *r,int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void get_sa(int *r,int n,int m)
{
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;
for(j=1,p=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;
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];
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++;
}
}
void get_height(int *r,int n)
{
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++);
}
int solve(int k,int n)
{
int maxx=0,minn=inf,ans=0;
for(int i=1;i<=n;i++)
if(height[i]<k)
{
if(maxx-minn>=k)
{
ans++;
//printf("%d %d\n",maxx,minn);
}
maxx=0,minn=inf;
}
else
{
maxx=max(max(sa[i-1],sa[i]),maxx);
minn=min(min(sa[i-1],sa[i]),minn);
}
if(maxx-minn>=k)ans++;
return ans;
}
int main()
{
while(scanf("%s",str),str[0]!='#')
{
int n=strlen(str);
for(int i=0;i<n;i++)
num[i]=str[i]-'a'+1;
num[n]=0;
int ans=0,m=30;
get_sa(num,n+1,m);
get_height(num,n);
//for(int i=0;i<=n;i++)
// printf("i==%d,sa==%d,height==%d\n",i,sa[i],height[i]);
for(int i=1;i<=n/2;i++)
ans+=solve(i,n);
printf("%d\n",ans);
}
return 0;
}