ARC110E Shorten ABC

ARC110E Shorten ABC

考虑操作的本质,将 A A A B B B C C C 分别记作 1 1 1 2 2 2 3 3 3

操作 s i s_i si s i + 1 s_{i+1} si+1 即合并为 s i ⊕ s i + 1 s_i \oplus s_{i+1} sisi+1

那么区间的异或和并不变,对于异或和为 0 0 0 的区间,最后只会全部相同;否则一定剩下一个字符(奇数且全部相同的除外)。

推论,对于异或和为 0 0 0 的区间,若在它左或右边多加上一个字符,一定会合并成一个字符。

对于一种操作得到的字符串 t t t,相等于将原串前面全部分成异或和不为 0 0 0 的段,且满足最后一段异或和为 0 0 0(空串也算)。

普通 DP 具备重复性,重复是因为异或和为 0 0 0 的段可能有多种可能方案。

于是考虑去除异或和为 0 0 0 的段的贡献即不枚举其,考虑当前段结尾为 i i i,找到它右边第一个 j j j,满足区间 [ 1 , j ] [1,j] [1,j] 的前缀异或和 ≠ \ne = 区间 [ 1 , i ] [1,i] [1,i] 的异或和,即表示区间 [ i + 1 , j ] [i+1,j] [i+1,j] 的异或和不为 0 0 0

f i f_i fi 表示按上述方法分段,最后一段以 i i i 结尾的方案数。

预处理上述方法的递推顺序即可。

答案即为满足 [ i + 1 , n ] [i+1,n] [i+1,n] 的异或和为 0 0 0 的所有 i i i 对应的 f i f_i fi 之和。

时间复杂度 O ( n ) \mathcal O(n) O(n)

类似的练手题:AT4379 [AGC027E] ABBreviate

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define ha putchar(' ')
#define he putchar('\n')

inline int read() {
	int x = 0, f = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-')
			f = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9')
		x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
	return x * f;
}

inline void write(int x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (x > 9)
		write(x / 10);
	putchar(x % 10 + 48);
}

const int _ = 1e6 + 10, mod = 1e9 + 7;

int n, s[_], nxt[_][4], f[_];

char c[_];

signed main() {
	n = read();
	scanf("%s", c + 1);
	bool flg = 0;
	for (int i = 1; i < n; ++i)
		if (c[i] != c[i + 1]) {
			flg = 1;
			break;
		}
	if (!flg) return puts("1"), 0;
	for (int i = 1; i <= n; ++i) s[i] = s[i - 1] ^ (c[i] - 'A' + 1);
	for(int i = 0; i < 4; ++i) nxt[n][i] = n + 1;
	for (int i = n; i >= 1; --i) {
		for (int j = 0; j < 4; ++j) nxt[i - 1][j] = nxt[i][j];
		nxt[i - 1][s[i]] = i;
	}
	f[0] = 1;
	ll ans = 0;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < 4; ++j)
			if(s[i] != j) f[nxt[i][j]] = (f[nxt[i][j]] + f[i]) % mod;
		if(s[i + 1] == s[n]) ans = (ans + f[i + 1]) % mod;
//		cout << f[i + 1] << "\n";
	}
	write(ans), he;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PS D:\pythonProject> pip install matplotlib Collecting matplotlib Obtaining dependency information for matplotlib from https://files.pythonhosted.org/packages/d0/39/4c4fb38ec2356bcbc9017a5421623aec69aacde110e4e76d34d0a43702f0/matplotlib-3.7.2-cp310-cp310-win32.whl.metadata Using cached matplotlib-3.7.2-cp310-cp310-win32.whl.metadata (5.8 kB) Collecting contourpy>=1.0.1 (from matplotlib) Using cached contourpy-1.1.0.tar.gz (13.4 MB) Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... error error: subprocess-exited-with-error × Preparing metadata (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [25 lines of output] + meson setup C:\Users\张豪\AppData\Local\Temp\pip-install-fuirjkwl\contourpy_ab1b9965df3941b882963f3350114322 C:\Users\张豪\AppData\Local\Temp\pip-install-fuirjkwl\contourpy_ab1 b9965df3941b882963f3350114322\.mesonpy-qrhmsqrm\build -Dbuildtype=release -Db_ndebug=if-release -Db_vscrt=md --vsenv --native-file=C:\Users\张豪\AppData\Local\Temp\pip-install-fuirjkwl\contourpy_ab1b9965df3941b882963f3350114322\.mesonpy-qrhmsqrm\build\meson-python-native-file.ini The Meson build system Version: 1.2.0 Source dir: C:\Users\张豪\AppData\Local\Temp\pip-install-fuirjkwl\contourpy_ab1b9965df3941b882963f3350114322 Build dir: C:\Users\张豪\AppData\Local\Temp\pip-install-fuirjkwl\contourpy_ab1b9965df3941b882963f3350114322\.mesonpy-qrhmsqrm\build Build type: native build Project name: contourpy Project version: 1.1.0 Activating VS 17.6.5 note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details.
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值