Codeforces-804 B. Minimum number of steps(贪心+规律)
来源:互联网 发布:微信预约挂号系统源码 编辑:程序博客网 时间:2024/05/01 04:38
We have a string of letters ‘a’ and ‘b’. We want to perform some operations on it. On each step we choose one of substrings “ab” in the string and replace it with the string “bba”. If we have no “ab” as a substring, our job is done. Print the minimum number of steps we should perform to make our job done modulo 109 + 7.
The string “ab” appears as a substring if there is a letter ‘b’ right after the letter ‘a’ somewhere in the string.
Input
The first line contains the initial string consisting of letters ‘a’ and ‘b’ only with length from 1 to 106.
Output
Print the minimum number of steps modulo 109 + 7.
Examples
Input
ab
Output
1
Input
aab
Output
3
Note
The first example: “ab” → “bba”.
The second example: “aab” → “abba” → “bbaba” → “bbbbaa”.
解题思路:
可以这样想,如果题目要求是把“ab”转换成“ba”的话,就相当于把所有的b移到前面去,把a移到后面去。但是现在在转换的过程中增添了一个‘b’,因此在逆序的时候,每次遇到一个“b”就把累积的的变量加1,如果遇到”a”,就把累积变量累加的到总和里,并且把累积变量乘以2,因为每次转换都会把a移动到后面,而前面留下了两个b,对前面的a造成影响。
#include<iostream>#include<cstring>using namespace std;#define mod 1000000007char a[1000005];int main(){ while(cin>>a) { int len=strlen(a),ans=0,sum=0; for(int i=len-1;i>=0;i--) { if(a[i]=='b') sum=(sum+1)%mod; else { ans=(ans+sum)%mod; sum=(sum*2)%mod; } } cout<<ans<<endl; } return 0;}
- Codeforces-804 B. Minimum number of steps(贪心+规律)
- codeforces 804B Minimum number of steps
- codeforces Minimum number of steps (804 B)
- codeforces 804B——Minimum number of steps(模拟,贪心)
- 【CodeForces 804B】Minimum number of steps(思维+数学)
- Codeforces Round #411 D. Minimum number of steps (贪心。)
- CodeFroces 804B Minimum number of steps
- codeforce 804B Minimum number of steps
- codeforce 804B Minimum number of steps
- CF#804 B. Minimum number of steps(字符串,思维)
- B. Minimum number of steps
- 【Codeforces Round #411 (Div. 1)】Codeforces 804B Minimum number of steps
- codeforces 805D Minimum number of steps (水题)
- Codeforces 805D Minimum number of steps
- CodeForces 805D Minimum number of steps
- codeforces Minimum number of steps 思维
- 【Codeforces 805】D. Minimum number of steps
- Minimum number of steps
- 数字三角形
- Mac上搭建直播服务器Nginx+rtmp
- mysql开启binlog日志后重启失败
- ThreadLocal
- 哈佛幸福课-完美主义
- Codeforces-804 B. Minimum number of steps(贪心+规律)
- 实验5
- 基于MongoDB REST API 以及XMPP的游戏数据同步架构
- 空心圆和实心圆
- Mac直播服务器Nginx配置对HLS的支持
- React Js 如何引用 img src require
- c++实验5-项目1
- 一个滚动监控器实例
- 工作流3个个问题