题解——CF804B Minimum number of steps
Hacker_Zcat · · 题解
题意
这个题是一道不难的数论题,它其实相当于在问:我们最少需要多少次把所有的'a'移动到队尾。
那我们就只需要计算每个'a'后有多少个'b'。
好了多的不说了,请看代码。
AC code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M = 1e9 + 7;
string s;
ll n, cnt, ans;//初始化计数器 cnt 和结果变量 ans 为 0。
int main(){
ios::sync_with_stdio(false);//加速代码
cin.tie(0);
cout.tie(0);
cin >> s;
n = s.length();//字符串的长度。
s = ' ' + s;//在前面补空格,方便计算。
for(ll i = n; i >= 1; i--){
if(s[i] == 'b'){
cnt = (cnt + 1) % M;
//遇到 'b' 时,cnt 加 1
}
else{
ans = (ans + cnt) % M;
cnt = (cnt * 2) % M;
//遇到 'a' 时,将 cnt 的值加到 ans 中(表示这个 'a' 需要移动其后的所有 'b'),然后将 cnt 加倍
}
}
cout << ans % M;//输出最小操作步数模M的值
return 0;
}