HDU 4909 String(组合数学)

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值