hdu 4910 String && BestCoder Round #3
来源:互联网 发布:怎么才可以做淘宝模特 编辑:程序博客网 时间:2024/06/08 00:28
题意:找到一个字符串中所有的只包含偶数个相同字符(小写字母)的子串的个数(?可以代表任何字母)
分析&题解:1.通过前缀和的思想来找到所有情况。
2.因为只需要判断奇偶,所以只需要记录同样奇偶性的前缀,可以用状态压缩来表示,开一个(1 << 26)大
的数组
3.对于没有?的情况,比如具有同样的前缀个数为n,那么以此前缀开始的满足条件的子串数为C(n,2);
4.在有?的情况下,需要枚举?为a,b,c,d...z,null,的所有情况
5.但是只存在于?前边与只存在于?后边的子串有重复,需要再减去
6.开这么大的数组再memset会超时超空间,所以每次只需要清空用过的值
7.发现测评系统算内存消耗的时候只测用过的内存,这个题只要开80000000一下的内存都能过。
总结:这个题做了n久终于磨出来了,题目感觉真心不错,总结就是要一直明确所有数组,变量的作用和含义!
#include<iostream>#include<cstring>#include<cstdio>using namespace std;#define MAXN 20005#define MAXM 67108864char s[MAXN];int hash,map[MAXM];void clear(int l,int r){ int bit = map[0] = 0; for(int i = l;i < r;i++)if(s[i] != '?') { bit ^= 1 << (s[i] - 'a'); map[bit] = 0; }}int run(int l,int r){ clear(l,r); int ans = hash = 0; map[0] = 1; for(int i = l;i < r;i++) { if(s[i] != '?') hash ^= 1 << s[i] - 'a'; ans += map[hash]; map[hash]++; } return ans;}int main(){ int _; scanf("%d",&_); while(_--) { scanf("%s",s); int len = strlen(s),pos; for(pos = 0;pos < len;pos++) if(s[pos] == '?')break; int ans = run(0,len); int cut = run(0,pos) + run(pos + 1,len); for(int i = 0;i < 26;i++) { s[pos] = i + 'a'; ans += run(0,len); } printf("%d\n",ans - 26 * cut); }}
0 0
- hdu 4910 String && BestCoder Round #3
- [BestCoder Round #3] hdu 4909 String (状压,计数)
- [BestCoder Round #3] hdu 4910 Problem about GCD
- HDU-5284-wyh2000 and a string problem(BestCoder Round #48 ($))
- BestCoder Round #89 1001 /hdu 5944/ Fxx and string
- [BestCoder Round #3] hdu 4908 BestCoder Sequence (计数)
- hdu 4908 BestCoder Sequence && BestCoder Round #3 1002
- HDU 4908 BestCoder Sequence——BestCoder Round #3
- BestCoder round#3 1001 && Hdu 4907
- HDU 4907 Task schedule(BestCoder Round #3 )
- BestCoder Round #3 BestCoder Sequence
- HDU BestCoder Round #48 1001
- hdu 5641 BestCoder Round #75
- hdu 5643 BestCoder Round #75
- BestCoder Round #83(HDU 5680)
- HDU 4907 - Task schedule(BestCoder Round #3 简单DP)
- [BestCoder] Round #3 1001、1002 | HDU 4907、4908
- BestCoder Round #3 1001 && HDU 4907 Task schedule (预处理)
- ListView中item点击事件、item保持选中状态以及其他细节
- HDU 1796 How many integers can you find 容斥原理
- linux删除大文件的前n行
- Leetcode--Valid Parentheses
- OpenVPN协议解析-网络结构之外
- hdu 4910 String && BestCoder Round #3
- 计时器
- Gson解析json
- 最大子矩阵求和 NYOJ 104 && 372 && HDU 1081
- Git入门教程
- MFC学习笔记——static text 和 picture control
- 搜索推广营销五步曲之四 上线实施
- 类的加载顺序
- 生成模型与判别模型