How to find the minimum number of steps in CodeForces 805D problem?

2026-06-09 12:382阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计319个文字,预计阅读时间需要2分钟。

How to find the minimum number of steps in CodeForces 805D problem?

题目链接:http://codeforces.com/contest/805/problem/D

题目内容:给你一个字符串,你需要对其进行操作,直到不能再操作为止。操作的规则是,遇到字符串中的ab就将其变为bba。你的目标是求出最少需要进行多少次操作。


题目链接:​​codeforces.com/contest/805/problem/D​​​
题意:给你一个字符串,你要对他进行操作,直至不能操作为止,操作的结果是,遇到ab字符串就把他变成bba,问你最少需要多少次操作
解析:其实变几个你大概就能发现规律了,ab->bba,abb->bbab->bbbba,你会发现a后面跟着几个b,那就需要变几次,而且变过以后,b的个数会翻一倍,并且放到了a的前面,所以会对前面有影响,于是你从后往前面看就好了

How to find the minimum number of steps in CodeForces 805D problem?

#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
int main(void)
{
string a;
cin>>a;
int len = a.length();
int cnt = 0,ans = 0;
for(int i=len-1;i>=0;i--)
{
if(a[i]=='a')
{
ans = (ans+cnt)%mod;
cnt = (cnt*2)%mod;
}
else
cnt++;
}
printf("%d\n",ans);
return 0;
}


本文共计319个文字,预计阅读时间需要2分钟。

How to find the minimum number of steps in CodeForces 805D problem?

题目链接:http://codeforces.com/contest/805/problem/D

题目内容:给你一个字符串,你需要对其进行操作,直到不能再操作为止。操作的规则是,遇到字符串中的ab就将其变为bba。你的目标是求出最少需要进行多少次操作。


题目链接:​​codeforces.com/contest/805/problem/D​​​
题意:给你一个字符串,你要对他进行操作,直至不能操作为止,操作的结果是,遇到ab字符串就把他变成bba,问你最少需要多少次操作
解析:其实变几个你大概就能发现规律了,ab->bba,abb->bbab->bbbba,你会发现a后面跟着几个b,那就需要变几次,而且变过以后,b的个数会翻一倍,并且放到了a的前面,所以会对前面有影响,于是你从后往前面看就好了

How to find the minimum number of steps in CodeForces 805D problem?

#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
int main(void)
{
string a;
cin>>a;
int len = a.length();
int cnt = 0,ans = 0;
for(int i=len-1;i>=0;i--)
{
if(a[i]=='a')
{
ans = (ans+cnt)%mod;
cnt = (cnt*2)%mod;
}
else
cnt++;
}
printf("%d\n",ans);
return 0;
}