题解:P11965 [GESP202503 七级] 等价消除
__xxy_free_ioi__ · · 题解
P11965 [GESP202503 七级] 等价消除
好题。。。
解法
因为只有 26 个字母,所以考虑状压。设转态
- 若
dp_{i,j} = 0 ,则方案数 + 1。 - 若
dp_{1,i-1} = dp_{1,j} ,则dp_{i, j} = dp_{1,i-1} - dp_{1,j} = 0 。 -
dp_{1, i} = dp_{1,i} \oplus 2^{S_i-'a'}
所以,我们发现
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int S = 1 << 26;
ll n, res, dp;
ll f[S];
string s;
int main() {
cin >> n >> s;
for (int i = 0; i < n; i++) {
dp ^= (1 << (s[i] - 'a'));
if (dp == 0) res++;
res += f[dp];
f[dp]++;
}
cout << res << '\n';
return 0;
}