跳到正文

学习·文化课

暑假81道题-第五题u540458

Luna_From_Starry约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年6月30日
收录
2025年12月12日
更新
2026年10月27日
篇幅
约 2 分钟

版本

  1. v1当前

前言\boxed{\text{前言}}

不知道第几周的题了

writer,coder,jcer all Luna_From_Starry\textcolor{#52c41a}{\mathrm{Luna\_From\_Starry}}

U540458 -a-b-c-

今天终于又轮到晓莱出题目了。

题目规则非常的简单,直接给定一个只由AA, BB, CC构成的字符串SS。

晓莱希望在SS字符串上执行以下操作:

选择一个连续的 SS 的子字符串,如果该字符串是 ABC 将其替换为 BCA。

请你输出最多可以执行多少次操作。

输入格式

输入一个字符串SS。

思路\boxed{\text{思路}}

一共就-种情况 1.AA…ABCBCBC…BCAA\dots ABCBCBC\dots BC

AC Code\boxed{\text{AC Code}}

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;
const int M = 1e6 + 5;
const int INF = 1e18;
int n;
string s;
signed main(){
//	freopen("T1.in", "r", stdin);
//	freopen("T1.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> s;
	n = s.size();
	s = " " + s;
	int ans = 0,x = 0;
	for(int i = 1; i <= n; i++){
		if(s[i] == 'A'){
			int j = i,y = 0;
			while(j <= n && s[j] == 'A'){
				j++;
				x++;
			} 
			while(j + 1 <= n && s[j] == 'B' && s[j + 1] == 'C'){
				j += 2;
				y++;
			}
			ans += x * y;
			i = j - 1;
		}else{
			x = 0;
		}
	}
	cout << ans << "\n";
	return 0;
}
原文