Boring counting
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3534 Accepted Submission(s): 1468
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
要求计算出现两次且不重叠的子串个数。
枚举长度,把height大于等于长度的分为一组,计算每组的sa值范围[L,R].若范围大于等于长度,说明可以找到不重叠的一组答案。
#include <cstdio>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=1005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
char s[maxn];
int wa[maxn],wb[maxn],wv[maxn],ws[maxn],sa[maxn],ranki[maxn],height[maxn];
int a[maxn];
int cmp(int *r,int a,int b,int l) {
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void build(int *r,int *sa,int n,int m) {
int i,j,k,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=0;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];
t=x;x=y;y=t;
p=1;x[sa[0]]=0;
for (i=1;i<n;i++) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
for (i=1;i<n;i++) ranki[sa[i]]=i;k=0;
for (i=0;i<n-1;height[ranki[i++]]=k) {
if (k) k--;
for (j=sa[ranki[i]-1];r[i+k]==r[j+k];k++);
}
}
int main() {
scanf("%s",s);
while (s[0]!='#') {
int i,j,len;
ll ans=0;
len=strlen(s);
for (i=0;i<len;i++) a[i]=s[i]-'a'+1;
a[len]=0;
build(a,sa,len+1,27);
for (i=1;i<=len/2;i++) {
int L,R;
L=R=sa[1];
for (j=2;j<=len;j++) {
if (height[j]>=i) {
L=min(L,sa[j]);R=max(R,sa[j]);
} else {
if (R-L>=i) ans++;
L=R=sa[j];
}
}
if (R-L>=i) ans++;
}
printf("%lld\n",ans);
scanf("%s",s);
}
return 0;
}