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

 

提示

You can perform the operations three times as follows: ABCABC → BCAABC → BCABCA → BCBCAA. This is the maximum result.

题意

在一个只包含A、B、C的字符串,有一种操作,可使 “ABC” 变成 ”BCA“,求字符串s的最多操作数。

1≤∣s∣≤200000

思路

  易得,该操作是将A与BC交换位置,可用 1、0分别代表“A”、“BC”。题意转化对一个只包含10的序列,将所有的10更新01,即将所有的0放在1前面。假设序列中共有k个0,每个0前面有ai​个1,则ans=\sum_{i=1}^{k}ai

  对于单独B、C,则可看作是两个序列分隔的标志。

 

 

 

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值