专栏文章
B4218
B4218题解参与者 3已保存评论 3
文章操作
快速查看文章及其快照的属性,并进行相关操作。
- 当前评论
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @minrv4q2
- 此快照首次捕获于
- 2025/12/02 07:20 3 个月前
- 此快照最后确认于
- 2025/12/02 07:20 3 个月前
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 代码
CPP#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;
}
相关推荐
评论
共 3 条评论,欢迎与作者交流。
正在加载评论...