B4218
Nostopathy · · 题解
0 知识铺垫
String.find(const string& s, int pos = 0):从下标 pos 开始寻找字符串 s 出现位置,没找到返回 -1。
String.replace(size_t pos, size_t len, const string& str):将 pos 后长度为 len 的子串替换为 str。
1 解题思路
使用整体思想:将 BC 替换为新的表示 D。
则思路转换为统计连续的 A、D 数量。
使用变量 A 个数,出现 D 则可与以前的每一个 A 进行操作,答案应加上 B 或 C 则不能进行操作,应将
对于连续 A 拼接连续 B,对答案贡献为
2 AC 代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
string s; cin>>s;
int p=0;
while((p=s.find("BC", p))!=-1) s.replace(p++, 2, "D");
int sa=0, ans=0;
for(char c: s){
if(c=='A') ++sa;
else if(c=='D') ans+=sa;
else sa=0;
}
cout<<ans;
}