标签:color csharp one figure bsp without printf his strlen
[HDU3518]Boring counting
试题描述
输入
输出
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\n", ans); } return 0; }
标签:color csharp one figure bsp without printf his strlen
原文地址:http://www.cnblogs.com/xiao-ju-ruo-xjr/p/6512871.html