2016多校第四场 HDU5769
来源:互联网 发布:企鹅媒体平台登录mac 编辑:程序博客网 时间:2024/06/01 13:37
给你一个字符x 和一个串s ,让你找s 有多少不同的子串包含字符x 设s1,s2 ,当其长度不同或者某位字符不同则不同。
此类题一般都是要枚举一维,然后往后找,每次加前面未出现过的新串。为了方便去重,这道题可以先用后缀数组处理出来sa,height 数组。然后从字典序小的开始往大的找。把x 出现的位置丢进可变长数组v ,可以构成合法串的开始位置就是x 的位置id 为了与之前的不重复,利用height 的性质可以很快知道sa[i−1]与sa[i] 的最长公共前缀,id 与起位置去最大值就是不重复的合法位置了。
//只是为了贴个后缀数组。写的博客//// Created by Running Photon// Copyright (c) 2015 Running Photon. All rights reserved.//#include <algorithm>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <map>#include <queue>#include <string>#include <sstream>#include <set>#include <vector>#include <stack>#define ALL(x) x.begin(), x.end()#define INS(x) inserter(x, x,begin())#define ll long long#define CLR(x) memset(x, 0, sizeof x)using namespace std;const int inf = 0x3f3f3f3f;const int MOD = 1e9 + 7;const int maxn = 1e6 + 10;const int maxv = 1e3 + 10;const double eps = 1e-9;int sa[maxn], rak[maxn], height[maxn];int wa[maxn], wb[maxn], wv[maxn], c[maxn];bool cmp(int *r, int a, int b, int l) { return r[a] == r[b] && r[a + l] == r[b + l];}//r是需要处理的数组,n是r的长度+1,r[len] = 0;void calcSa(int *r, int *sa, int n, int m) { int i, j, p, *x = wa, *y = wb; for(i = 0; i < m; i++) c[i] = 0; for(i = 0; i < n; i++) c[x[i] = r[i]]++; for(i = 1; i < m; i++) c[i] += c[i-1]; for(i = n - 1; ~i; i--) sa[--c[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++) c[i] = 0; for(i = 0; i < n; i++) c[wv[i]]++; for(i = 1; i < m; i++) c[i] += c[i-1]; for(i = n - 1; i >= 0; i--) sa[--c[wv[i]]] = y[i]; swap(x, y); 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++; if(p >= n) break; }}//r是需要处理的数组,n是r的长度void calcHeight(int *r, int *sa, int n) { int i, j, p = 0; for(i = 1; i <= n; i++) rak[sa[i]] = i; for(i = 0; i < n; i++) { j = sa[rak[i] - 1]; while(r[i+p] == r[j+p]) p++; height[rak[i]] = p; if(p > 0) p--; }}char s[maxn];int r[maxn];int main() {#ifdef LOCAL freopen("C:\\Users\\Administrator\\Desktop\\in.txt", "r", stdin); freopen("C:\\Users\\Administrator\\Desktop\\out.txt","w",stdout);#endif// ios_base::sync_with_stdio(0); int T; int cas = 0; scanf("%d", &T); while(T--) { char t[10]; scanf("%s%s", t, s); int len = strlen(s); std::vector<int> v; for(int i = 0; i < len; i++) { if(s[i] == t[0]) v.push_back(i); r[i] = s[i] - 'a' + 1; } r[len] = 0; calcSa(r, sa, len + 1, 30); calcHeight(r, sa, len); // for(int i = 1; i <= len; i++) { // printf("height[%d] = %d\n", i, height[i]); // } ll ans = 0; for(int i = 1; i <= len; i++) { auto iter = lower_bound(ALL(v), sa[i]); if(iter == v.end()) continue; int id = *iter; // printf("i = %d id = %d\n", i, id); id = max(id, sa[i] + height[i]); // printf("id = %d\n", id); ans += len - id; } printf("Case #%d: %lld\n", ++cas, ans); } return 0;}
0 0
- 2016多校第四场 HDU5769
- (多校第四场1006)HDU5769 Substring 后缀数组
- 2016HDU多校赛第4场(hdu5765 hdu5769 hdu5772)
- 多校第四场
- 多校第四场
- 2016多校联合第四场 HDU5768
- hdu5773 --2016多校第四场1010
- 2013 多校第四场
- 多校联赛第四场
- 2016多校训练Contest4: 1006 Substring hdu5769
- hdu5774(2016多校第四场,水题)
- hdu 5769 Substring 2016 多校第四场
- 2012多校第四场总结
- 2013多校联合训练第四场
- hdu 4639 Hehe 多校第四场
- hdu 4638 Group 多校第四场
- HDOJ多校联合第四场
- 2015 多校第四场 Walk Out
- 放在Program Files 目录下的exe 执行开起写入权限
- 虚幻4增强现实Demo
- secureCRT 里使用 python 按 backspace 键删除出现 ^H
- POJ Pots 3414 (bfs)
- Selenium2学习笔记二
- 2016多校第四场 HDU5769
- Lua os标准库 - 操作系统处理(operating system facilities)
- ②BeautifulSoup基础知识
- 笔记13 RecycleView
- WPAD
- 二叉树相隔最远的俩点距离
- OpenCV 特征描述
- jquery验证框架
- 题目277 车牌号