HDU 4909 String(组合数学)
来源:互联网 发布:java 事物的特征 编辑:程序博客网 时间:2024/06/06 14:27
HDU 4909 String
题目链接
题意:给定一个字符串全是小写字符,可能有一个位置为?,问号可以替代任何字符,也可以删掉,问有多少连续字串满足所有字母是偶数个
思路:组合数学,计算所有前最串的各个字母的奇偶状态,用一个01串表示,然后记录下个数,对于每个相同的状态,任选两个就能得到一个子序列,答案为所有C(num, 2)的和。
但是这个问题多了一个?的情况,但是没关系,可以枚举?,然后把序列分为3部分去考虑,?之前,?之后,和包含了?的串分开考虑即可
代码:
#include <cstdio>#include <cstring>#include <map>using namespace std;typedef int ll;const int N = 20005;int t, n, v, x;char str[N];map<int, int> hash;int main() { scanf("%d", &t); while (t--) {scanf("%s", str);v = -1;n = strlen(str);for (int i = 0; i < n; i++) { if (str[i] == '?') {v = i;break; }}int ans = 0;if (v == -1) { x = 0; hash.clear(); hash[0]++; for (int i = 0; i < n; i++) {x ^= (1<<(str[i] - 'a'));ans += hash[x];hash[x]++; } printf("%d\n", ans); continue;}else { hash.clear(); ans = 0; x = 0; hash[0]++; for (int i = 0; i < v; i++) {x ^= (1<<(str[i] - 'a'));ans += hash[x];hash[x]++; } hash.clear(); x = 0; hash[0]++; for (int i = v + 1; i < n; i++) {x ^= (1<<(str[i] - 'a'));ans += hash[x];hash[x]++; } hash.clear(); x = 0; hash[0]++; for (int i = v + 1; i < n; i++) {x ^= (1<<(str[i] - 'a'));hash[x]++; } x = 0; if (hash.count(x))ans += hash[x]; for (int j = 0; j < 26; j++) {if (hash.count(x^(1<<j))) ans += hash[x^(1<<j)]; } for (int i = v - 1; i >= 0; i--) {x ^= (1<<(str[i] - 'a'));if (hash.count(x)) ans += hash[x];for (int j = 0; j < 26; j++) { if (hash.count(x^(1<<j))) {ans += hash[x^(1<<j)]; }} }}printf("%d\n", ans); } return 0;}
1 0
- HDU 4909 String(组合数学)
- hdu 4373 组合数学
- hdu 3944 组合数学
- hdu 4045 组合数学
- HDU -1261 组合数学。
- HDU Candy(数学 组合)
- HDU 1027 组合数学
- hdu 1220组合数学
- HDU 5651 组合数学
- HDU 5698 组合数学
- HDU 5894 组合数学
- hdu 4602 组合数学
- HDU-3037 (组合数学)
- HDOJ 3398 String -- 组合 数学
- HDU 3944 组合数学+数论
- hdu 1249 三角形 组合数学
- hdu 1220 Cube (组合数学)
- HDU 5159 Card (组合数学)
- Hadoop2.4.0伪分布安装步骤
- activeX 组件修改 ——提高网络安全性
- Paste
- hdu1087 Super Jumping! Jumping! Jumping!
- Jquery Table 的基本操作
- HDU 4909 String(组合数学)
- 心情实验
- HDOJ 题目3032Nim or not Nim?(sg博弈)
- Android SDK Manager 无法下载 SDK 问题?
- gvim+ctags+taglist带自动提示功能的代码编辑利器
- 请教大师!如何用 scanf 正确输入数据?
- 用angularjs开发下一代web应用(二):angularjs应用骨架(一)
- 命令行之俄罗斯方块
- leetcode