[HDU3518]Boring counting
试题描述
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”).
输入
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).
输出
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.
输入示例
aaaa
ababcabb
aaaaaa
#
输出示例
2 3 3
数据规模及约定
见“输入”
题解
数据范围比较小,算出 height 数组之后暴力数就好了。注意你需要打一个 tag[i] 的标记表示对于 i 这个后缀你已经考虑过它的长度小于等于 tag[i] 的前缀了,这样可以避免重复计数,也能保证复杂度。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <algorithm> #define maxn 1010 char S[maxn]; int n, rank[maxn], height[maxn], sa[maxn], Ws[maxn]; bool cmp(int* a, int p1, int p2, int l) { if(p1 + l > n && p2 + l > n) return a[p1] == a[p2]; if(p1 + l > n || p2 + l > n) return 0; return a[p1] == a[p2] && a[p1+l] == a[p2+l]; } void ssort() { int *x = rank, *y = height; int m = 0; memset(Ws, 0, sizeof(Ws)); for(int i = 1; i <= n; i++) Ws[x[i] = S[i]]++, m = std::max(m, x[i]); for(int i = 1; i <= m; i++) Ws[i] += Ws[i-1]; for(int i = n; i; i--) sa[Ws[x[i]]--] = i; for(int j = 1, pos = 0; pos < n; j <<= 1, m = pos) { pos = 0; for(int i = n - j + 1; i <= n; i++) y[++pos] = i; for(int i = 1; i <= n; i++) if(sa[i] > j) y[++pos] = sa[i] - j; for(int i = 1; i <= m; i++) Ws[i] = 0; for(int i = 1; i <= n; i++) Ws[x[i]]++; for(int i = 1; i <= m; i++) Ws[i] += Ws[i-1]; for(int i = n; i; i--) sa[Ws[x[y[i]]]--] = y[i]; std::swap(x, y); pos = 1; x[sa[1]] = 1; for(int i = 2; i <= n; i++) x[sa[i]] = cmp(y, sa[i], sa[i-1], j) ? pos : ++pos; } return ; } void calch() { for(int i = 1; i <= n; i++) rank[sa[i]] = i; for(int i = 1, j, k = 0; i <= n; height[rank[i++]] = k) for(k ? k-- : 0, j = sa[rank[i]-1]; S[j+k] == S[i+k]; k++); return ; } int tag[maxn]; int main() { while(~scanf("%s", S + 1)) { if(S[1] == '#') break; n = strlen(S + 1); ssort(); calch(); memset(tag, 0, sizeof(tag)); int ans = 0; for(int i = 1; i <= n; i++) for(int len = tag[i] + 1; len <= n - sa[i] + 1; len++) { int cnt = 0, mnp = sa[i], mxp = sa[i]; for(int j = i + 1; j <= n && height[j] >= len; j++) { tag[j] = std::max(tag[j], len); mnp = std::min(mnp, sa[j]); mxp = std::max(mxp, sa[j]); } if(mxp - mnp >= len) ans++; } printf("%d ", ans); } return 0; }