石油大第十场--问题 B: ABC

题目描述

You are given a string s consisting of A, B and C.
Snuke wants to perform the following operation on s as many times as possible:
Choose a contiguous substring of s that reads ABC and replace it with BCA.
Find the maximum possible number of operations.
Constraints
1≤|s|≤200000
Each character of s is A, B and C.

输入

Input is given from Standard Input in the following format:
S

输出

Find the maximum possible number of operations.

样例输入

ABCABC

样例输出

3

思路

找到A右面有多少个连续BC整体,AC,BA,BB,CC都会阻断交换。从后往前找。

代码

#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
typedef long long ll;
const int N=2e5+5;
char s[N];
int main()
{
	scanf("%s",s);
	ll ans=0,pre=0;
	int len=strlen(s);
	for(int i=len-2;i>=0;i--)
	{
		if(s[i]=='A')
		{
			if(s[i+1]=='C')
				pre=0;
			ans+=pre;
		}
		else
		{
			if(s[i]=='B'&&s[i+1]=='C') pre++;
			if(s[i]=='B'&&s[i+1]=='B') pre=0;
			if(s[i]=='B'&&s[i+1]=='A') pre=0;
			if(s[i]=='C'&&s[i+1]=='C') pre=0;
		}
	}
	printf("%lld\n",ans);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值