arc110E Shorten ABC

30 篇文章 0 订阅
17 篇文章 0 订阅

Description

  • 给一个只包含’A’‘B’'C’的字符串 s s s,每一次可以选择相邻的两个不相同的字符将它们合成与它们不同的字符。
  • s s s经过任意次操作可以变成的不同的字符串有多少个
  • ∣ S ∣ ≤ 1 e 6 |S|\le1e6 S1e6

Solution

  • 与agc027E一样
  • 假设 A B C ABC ABC分别是 123 123 123,那么如果不考虑相邻的不同的话,那么操作就是将 x , y x,y x,y替换为 x   x o r   y x\ xor\ y x xor y,那么显然这个是满足结合律和交换律的。
  • 再考虑相邻的相同的情况,显然两个相同的也是可以相消的。
  • 讨论一下就会发现只需要把全部相同的情况特判掉,之后就可以转化为不考虑相邻不同的问题来做了。
  • 那么从一个 S S S变为 T T T,判断是否可行我们可以直接贪心,每一次选取最短的段,并且保证最后剩下的段 x o r xor xor和为0即可。
  • 那么直接DP,设 f [ i ] f[i] f[i]表示1~i的答案,枚举下一个是哪个字符,找到最前的位置转移过去即可。

  • agc027E是把 a b ab ab看作 12 12 12,在模意义下的加法,需要特判掉不存在相邻相同的情况,其他的证明讨论都是类似的。
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define maxn 1000005
#define mo 1000000007
using namespace std;

int n,i,j,k,a[maxn],f[maxn],s[maxn],nex[maxn][4],bz[4],ans;

int main(){
	freopen("ceshi.in","r",stdin);
	scanf("%d",&n); char ch=getchar();
	while (ch<'A'||ch>'C') ch=getchar();
	for(i=1;i<=n;i++) a[i]=ch-'A'+1,ch=getchar();
	for(k=1;k<=3;k++) {
		for(i=1;i<=n&&a[i]==k;i++);
		if (i>n) printf("1\n"),exit(0);
	}
	for(i=1;i<=n;i++) s[i]=s[i-1]^a[i];
	for(i=0;i<4;i++) bz[i]=n+1;
	for(i=n;i>=0;i--){
		memcpy(nex[i],bz,sizeof(bz));
		bz[s[i]]=i;
	}
	f[0]=1;
	for(i=0;i<=n;i++) if (f[i]){
		if ((s[n]^s[i])==0&&i>0) (ans+=f[i])%=mo;
		for(j=0;j<4;j++) if (j!=s[i]&&nex[i][j]<=n)
			(f[nex[i][j]]+=f[i])%=mo;
	}
	printf("%d\n",ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值