P11965 [GESP202503 七级] 等价消除 题解
jimmy9_666 · · 题解
题目传送门
这个蒟蒻赛事没把这道普及-写出来
题意
给一个长度为
求
分析
首先看
然后枚举每一个字串进行判断,统计有多少个子串
算法复杂度为
然后我们来考虑进行优化,仔细观察不难发现在判断每个字符串时,其实只与出现的次数的奇偶有关,所以我们可以考虑用
那么此时若要判断一个子串
然后我们再使用一个 map 来记录各状态的出现次数。然后在遍历字符串时,对于遍历到的每一位的状态 map 记录的状态的出现次数,然后将这个次数加一。
答案记得要使用 long long 存储。
代码
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#define LL long long
#define int long long
using namespace std;
int n;
string s;
map<int, int> m;
int ans;
signed main() {
cin >> n >> s;
m[0] = 1;
int x = 0;
for (int i = 0; i < n; i ++ ) {
x ^= (1 << (s[i] - 'a'));
ans += m[x];
m[x] ++ ;
}
cout << ans << endl;
return 0;
}