Codeforces Round #411 D. Minimum number of steps
来源:互联网 发布:怎么查看淘宝客佣金 编辑:程序博客网 时间:2024/05/17 06:27
题意
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
The string “ab” appears as a substring if there is a letter ‘b’ right after the letter ‘a’ somewhere in the string.
解题思路
根据题意,转换规则如下:
即对于任意一个 a
,与其直接相邻的连续 b 的个数将是其 replace
的次数,同时 b
的个数将翻倍。
同时可以想象,对于最终停止 replace
操作时的串的状态,必然是形如
故对于该问题,逆序处理该串,统计 b
的个数作为 cntb 。当找到一个 a
时,可以保证 cntb 个 b
必然与该 a
直接相连,对答案的贡献为 cntb 。同时 cntb 需倍乘。
代码
#include<bits/stdc++.h>using namespace std;const int mod = 1e9 + 7;char s[1000010];int main(){ scanf(" %s",s); int len = strlen(s); long long cntb = 0, ans = 0; for(int i=len-1;i>=0;--i) { if(s[i] == 'a') { (ans += cntb) %= mod; (cntb *= 2) %= mod; } else { cntb++; } } printf("%I64d\n", ans%mod);}
1 0
- Codeforces Round #411 D. Minimum number of steps
- Codeforces Round #411 (Div. 2) D. Minimum number of steps
- Codeforces Round #411 (Div. 2) D. Minimum number of steps
- Codeforces Round #411 (Div. 2) D. Minimum number of steps
- Codeforces Round #411 D. Minimum number of steps (贪心。)
- Codeforces Round #411(Div. 2)D. Minimum number of steps【思维递推+快速幂】
- Codeforces Round #411 D. Minimum number of steps (思维题)
- Codeforces Round #411 (Div. 2) D. Minimum number of steps(思维题目)
- Codeforces Round #411 D. Minimum number of steps【逆向思维+模拟】
- Codeforces 805D Minimum number of steps
- CodeForces 805D Minimum number of steps
- 【Codeforces 805】D. Minimum number of steps
- Codeforces Round #411(A. Fake NP; B. 3-palindrome; C. Find Amir; D.Minimum number of steps)
- 【Codeforces Round #411 (Div. 1)】Codeforces 804B Minimum number of steps
- codeforces 805D Minimum number of steps (水题)
- CF Round #411 (Div. 2) Minimum number of steps
- #411 Div.2 D. Minimum number of steps
- codeforces Minimum number of steps 思维
- SSL双向认证
- Intellij IDEA+Tomcat+JRebel热部署
- Codeforces Round #411 C. Find Amir
- 基于SSM框架maven管理的RAS密码加密算法
- [线段树] BZOJ 4821 [Sdoi2017]相关分析
- Codeforces Round #411 D. Minimum number of steps
- Codeforces805B. 3-palindrome
- ISMS
- HDU
- DRML(2016-CVPR)重现过程记录---(5)问题定位_1
- 页面加载初始化常用的三种方法
- [BZOJ 2811][Apio2012]Guard:贪心+二分
- 矩阵——洛谷P2461 [SDOI2008]递归数列
- python 自定义的模块和导入包