Codeforces 386C Diverse Substrings 计数问题
来源:互联网 发布:淘宝返利机器人的qq 编辑:程序博客网 时间:2024/06/05 01:06
题目大意:
就是给定一个长度不超过3*10^5的字符串, 含有的字符都是小写字母(‘a' ~ 'z'), 定义字符串的多样性d(s)为字符串中含有的不同字母的个数, 比如d("aaa") = 1, d("abbbc") = 3
对于给出的字符串求其所有子串中对应的d值为k的串的数量(子串只要起始位置或者终点位置不同就视作不同子串) (k从1到给出的串的d值, 对于这所有的k都要求出其数量)
大致思路:
看来是个很巧妙地计数问题, 现在一次来记录以位置 i 结尾的子串嘴硬的多样性d, 预先记录之前出现的26种字符最后出现的位置, 然后每次统计以 i 结尾的串时对当前前i个字符的26中字符的最后出现位置进行排序即可找到所有以i位置结尾的子串多样性为d的对应有多少个
细节见代码注释吧
代码如下:
Result : Accepted Memory : 1032 KB Time : 202 ms
/* * Author: Gatevin * Created Time: 2015/2/26 21:42:54 * File Name: poi~.cpp */#include<iostream>#include<sstream>#include<fstream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<iomanip>using namespace std;const double eps(1e-8);typedef long long lint;char s[300010];int pos[30];//记录各个字符最后出现的位置, 等于-1表示还没有出现过int p[30];lint ans[30];int main(){ scanf("%s", s); memset(pos, -1, sizeof(pos)); int n = strlen(s); for(int i = 0; i < n; i++) { pos[s[i] - 'a'] = i; int cnt = 0; for(int j = 0; j < 26; j++) if(pos[j] != -1) p[cnt++] = pos[j]; sort(p, p + cnt);//这个排序一定会使得当前的s[i]位置排在最后 for(int j = 1; j < cnt; j++)//统计起始位置[1 ~ i]到终点位置i的串的贡献 ans[cnt - j] += p[j] - p[j - 1];//多样性是cnt - j的刚好起点位置是(p[j - 1] ~ p[j]] ans[cnt] += p[0] + 1; } for(int i = 26; i >= 1; i--) if(ans[i]) { printf("%d\n", i); for(int j = 1; j <= i; j++) printf("%I64d\n", ans[j]); return 0; } return 0;}
0 0
- Codeforces 386C Diverse Substrings 计数问题
- CodeForces 386C Diverse Substrings
- CodeForces 386C. Diverse Substrings (字符串+双指针)
- codeforces C. Diverse Permutation
- 【CODEFORCES】C. Diverse Permutation
- codeforces 483C.Diverse Permutation
- CodeForces-483C Diverse Permutation
- CodeForces 483C Diverse Permutation
- C. Diverse Permutation(Codeforces Round #275(div2)
- CodeForces-483C Diverse Permutation(构造)
- Codeforces 483C Diverse Permutation【思维构造】
- Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)
- Codeforces Round #275 (Div. 2) C Diverse Permutation
- Codeforces Round #275 (Div. 2) --C Diverse Permutation
- Codeforces Round #275 (Div. 2) C. Diverse Permutation 构造
- Codeforces:Diverse Permutation(找规律)
- CodeForces 482A Diverse Permutation
- CodeForces 482A Diverse Permutation
- 【CodeForces】343D Water tree (线段树好题!还未弄懂)
- 工厂模式
- 蓝桥杯 串的简单处理 解题报告
- js eval函数使用,js对象和字符串互转.
- Android之网络访问
- Codeforces 386C Diverse Substrings 计数问题
- 初探Java序列化(2)-writeObject/readObject
- 角谷猜想---记忆化搜索
- iptables防火墙规则的添加、删除、修改、保存
- 备考ocp_SQL专题之DELETE
- 什么情况下需要覆铜
- mysql-01
- Excel合并单元格
- (十二)命令模式