Boring counting
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3003 Accepted Submission(s): 1248
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
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
using namespace std;
const int N=200010;
const int INF=0x7FFFFFFF;
struct Sa
{
char s[N];
int rk[2][N],sa[N],h[N],w[N],now,n;
int rmq[N][20],lg[N],bel[N];
bool GetS()
{
return scanf("%s",s+1),s[1]!='#';
}
void getsa(int z,int &m)
{
int x=now,y=now^=1;
for(int i=1; i<=z; i++) rk[y][i]=n-i+1;
for(int i=1,j=z; i<=n; i++)
if(sa[i]>z) rk[y][++j]=sa[i]-z;
for(int i=1; i<=m; i++) w[i]=0;
for(int i=1; i<=n; i++) w[rk[x][rk[y][i]]]++;
for(int i=1; i<=m; i++) w[i]+=w[i-1];
for(int i=n; i>=1; i--) sa[w[rk[x][rk[y][i]]]--]=rk[y][i];
for(int i=m=1; i<=n; i++)
{
int *a=rk[x]+sa[i],*b=rk[x]+sa[i-1];
rk[y][sa[i]]=*a==*b&&*(a+z)==*(b+z)?m-1:m++;
}
}
void getsa(int m)
{
now=rk[1][0]=sa[0]=s[0]=0;
n=strlen(s+1);
for(int i=1; i<=m; i++) w[i]=0;
for(int i=1; i<=n; i++) w[s[i]]++;
for(int i=1; i<=m; i++) rk[1][i]=rk[1][i-1]+(bool)w[i];
for(int i=1; i<=m; i++) w[i]+=w[i-1];
for(int i=1; i<=n; i++) rk[0][i]=rk[1][s[i]];
for(int i=1; i<=n; i++) sa[w[s[i]]--]=i;
rk[1][n+1]=rk[0][n+1]=0;
for(int x=1,y=rk[1][m]; x<=n&&y<=n; x<<=1) getsa(x,y);
for(int i=1,j=0; i<=n; h[rk[now][i++]]=j?j--:j)
{
if(rk[now][i]==1) continue;
int k=n-max(sa[rk[now][i]-1],i);
while(j<=k&&s[sa[rk[now][i]-1]+j]==s[i+j]) ++j;
}
}
void getrmq()
{
h[n+1]=h[1]=lg[1]=0;
for(int i=2; i<=n; i++)
rmq[i][0]=h[i],lg[i]=lg[i>>1]+1;
for(int i=1; (1<<i)<=n; i++)
{
for(int j=2; j<=n; j++)
{
if(j+(1<<i)>n+1) break;
rmq[j][i]=min(rmq[j][i-1],rmq[j+(1<<i-1)][i-1]);
}
}
}
int lcp(int x,int y)
{
int l=min(rk[now][x],rk[now][y])+1,r=max(rk[now][x],rk[now][y]);
return min(rmq[l][lg[r-l+1]],rmq[r-(1<<lg[r-l+1])+1][lg[r-l+1]]);
}
void work()
{
getsa(300);
int ans=0;
for(int i=1;i<=n;i++)
{
int ma=0,mi=INF;
for(int j=2; j<=n; j++)
{
if(h[j]>=i)
{
ma=max(max(sa[j-1],sa[j]),ma);
mi=min(min(sa[j-1],sa[j]),mi);
}
else
{
if(ma-mi>=i) ans++;
ma=0;mi=INF;
}
}
if(ma-mi>=i) ans++;
}
printf("%d\n",ans);
}
}sa;
int main()
{
while(sa.GetS()) sa.work();
return 0;
}