求有多少子串出现多次且不重叠。
SAM:考虑每个状态,发现知道其Right集合的最小值和最大值,以及max(s)和min(s),就可以算了。
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
#define FOR(i,j,k) for(i=j;i<=k;++i)
const int rt = 1, N = 10005;
int last, cnt, len;
int ch[N][26], fa[N], ma[N], rl[N], rr[N], b[N], dp[N], bucket[N];
char str[N];
void add(char c) {
int np = ++cnt, p = last, q, nq; last = np; rl[np] = rr[np] = ma[np] = ++len;
memset(ch[np], 0, sizeof ch[np]);
while (p && !ch[p][c]) ch[p][c] = np, p = fa[p];
if (!p) fa[np] = rt;
else {
q = ch[p][c];
if (ma[q] == ma[p] + 1) fa[np] = q;
else {
nq = ++cnt; memcpy(ch[nq], ch[q], sizeof ch[q]);
rl[nq] = 2147483647; rr[nq] = -1;
ma[nq] = ma[p] + 1; fa[nq] = fa[q]; fa[q] = fa[np] = nq;
while (p && ch[p][c] == q) ch[p][c] = nq, p = fa[p];
}
}
}
void sort(int *x, int *y, int n, int m) {
int i;
FOR(i,0,m) bucket[i] = 0;
FOR(i,1,n) ++bucket[x[i]];
FOR(i,1,m) bucket[i] += bucket[i - 1];
for(i=n;i;--i) y[bucket[x[i]]--] = i;
}
int main() {
int i, n; long long ans;
while (scanf("%s", str + 1) != EOF && str[1] != '#') {
last = cnt = 1; ans = len = 0;
memset(ch[rt], 0, sizeof ch[rt]);
n = strlen(str + 1);
FOR(i,1,n) add(str[i] - 'a');
sort(ma, b, cnt, n);
for(i=cnt;i;--i) {
rl[fa[b[i]]] = min(rl[fa[b[i]]], rl[b[i]]);
rr[fa[b[i]]] = max(rr[fa[b[i]]], rr[b[i]]);
}
FOR(i,1,cnt) if (rr[i] - rl[i] > ma[fa[i]])
ans += min(ma[i], rr[i] - rl[i]) - ma[fa[i]];
printf("%lld\n", ans);
}
return 0;
}
SA:分组height?半个月没动后缀数组一堆名词都忘了233
#include <cstdio>
#include <cstring>
#include <algorithm>
#define rep(i,j,k) for(i=j;i<k;i++)
#define FOR(i,j,k) for(i=j;i<=k;i++)
using std::min;
using std::max;
#define N 1005
int wa[N], wb[N], sa[N], bucket[N], height[N], rank[N], z[N];
char s[N];
void sort(int *x, int *y, int n, int m) {
int i;
rep(i,0,m) bucket[i]=0;
rep(i,0,n) bucket[x[y[i]]]++;
rep(i,1,m) bucket[i]+=bucket[i-1];
for(i=n-1;i>=0;i--) sa[--bucket[x[y[i]]]]=y[i];
}
void da(int n, int m) {
int i, j, p, *x=wa, *y=wb, *t;
rep(i,0,n) x[i]=s[i],z[i]=i;
sort(x,z,n,m);
for(j=1,p=1;p<n;j*=2,m=p) {
p=0;
rep(i,n-j,n) y[p++]=i;
rep(i,0,n) if(sa[i]>=j) y[p++]=sa[i]-j;
sort(x,y,n,m);
t=x,x=y,y=t;p=1;x[sa[0]]=0;
rep(i,1,n) x[sa[i]]=y[sa[i]]==y[sa[i-1]]&&y[sa[i]+j]==y[sa[i-1]+j]?p-1:p++;
}
}
void calHeight(int n) {
int i,j,k=0;
for(i=1;i<=n;i++) rank[sa[i]]=i;
rep(i,0,n) {
if(k) k--;
j=sa[rank[i]-1];
while(s[i+k]==s[j+k]) k++;
height[rank[i]]=k;
}
}
int main() {
while(scanf("%s", s) && s[0]!='#') {
int n=strlen(s),ans=0,h,j;
da(n+1,250); calHeight(n);
FOR(h,1,n/2) {
int l=n+2,r=-1;
FOR(j,2,n) {
if(height[j]>=h) {
l=min(min(sa[j],sa[j-1]),l);
r=max(max(sa[j],sa[j-1]),r);
} else {
if(l+h<=r) ans++;
l=n+2,r=-1;
}
}
if(l+h<=r) ans++;
}
printf("%d\n", ans);
}
return 0;
}
Boring counting
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2584 Accepted Submission(s): 1047
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