Contest1786 - 2019年第二阶段我要变强个人训练赛第十场 问题 B: ABC 思维

题目链接:http://icpc.upc.edu.cn/contest.php?cid=1786

题目描述

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 BB BA CC 都会阻断交换,BC则+1

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N = 200100;

char s[N];
int n;

int main() {

	while(~scanf("%s", s)) {
		n = strlen(s);
		ll ans = 0, pre = 0;
		for(int i = n - 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、付费专栏及课程。

余额充值