cf劲啊

原题:

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 10^9 + 7.

The string "ab" appears as a substring if there is a letter 'b' right after the letter 'a' somewhere in the string.

the initial string consisting of letters 'a' and 'b' only with length from 1 to 10^6.

大意:

给一个ab串,你每次能挑一个ab变成bba,问至少变多少次不能再变

串长1e6,答案膜1e9+7

b……bba?

我就是叫紫妈怎么了?

恩核心思路就是ab变成bba后相当于把a往右挪了一下并在a前面添加一个b

这样就好写了从右往左扫,如果遇到b就把b的个数+1,如果遇到a就把答案加上a后面b的个数并把b的个数翻倍

因为是从右往左扫的,这样就保证a往右挪的时候为后面的a添加的b的个数尽可能少,保证答案最小

不需要考虑一串a连在一起的情况,如果最右边全是a,那么不用管了对答案没贡献,就算左边的a挪过去也越不过这一串a

如果中间是一串a,那么右端点一定接了一个b,最右边的a挪过去后次右边又接上了b,这样就能保证所有的a都能挪到末尾

代码很好写,python13行(没压行,写的比较散

代码:

 mo =
a = raw_input()
b, c = ,
i = len(a) -
while i >= :
if a[i] == 'a':
c = (c + b) % mo
b = (b * ) % mo
else:
b = (b + ) % mo
i = i - print c

codeforce 804B Minimum number of steps的更多相关文章

  1. Codeforces 805D/804B - Minimum number of steps

    传送门:http://codeforces.com/contest/805/problem/D 对于一个由‘a’.‘b’组成的字符串,有如下操作:将字符串中的一个子串“ab”替换成“bba”.当字符串 ...

  2. Minimum number of steps CodeForces - 805D(签到题)

    D. Minimum number of steps time limit per test 1 second memory limit per test 256 megabytes input st ...

  3. Codeforces Round #411 div 2 D. Minimum number of steps

    D. Minimum number of steps time limit per test 1 second memory limit per test 256 megabytes input st ...

  4. Minimum number of steps 805D

    http://codeforces.com/contest/805/problem/D D. Minimum number of steps time limit per test 1 second ...

  5. Codeforces 805D - Minimum number of steps

    805D - Minimum number of steps 思路:简单模拟,a每穿过后面一个b,b的个数+1,当这个a穿到最后,相当于把它后面的b的个数翻倍.每个a到达最后的步数相当于这个a与它后面 ...

  6. Codeforces805D. Minimum number of steps 2017-05-05 08:46 240人阅读 评论(0) 收藏

    D. Minimum number of steps time limit per test 1 second memory limit per test 256 megabytes input st ...

  7. 【codeforces 805D】Minimum number of steps

    [题目链接]:http://codeforces.com/contest/805/problem/D [题意] 给你一个字符串; 里面只包括a和b; 让你把里面的"ab"子串全都去 ...

  8. 【推导】Codeforces Round #411 (Div. 1) B. Minimum number of steps

    最后肯定是bbbb...aaaa...这样. 你每进行一系列替换操作,相当于把一个a移动到右侧. 会增加一些b的数量……然后你统计一下就行.式子很简单. 喵喵喵,我分段统计的,用了等比数列……感觉智障 ...

  9. 【贪心】codeforces D. Minimum number of steps

    http://codeforces.com/contest/805/problem/D [思路] 要使最后的字符串不出现ab字样,贪心的从后面开始更换ab为bba,并且字符串以"abbbb. ...

随机推荐

  1. 每天CSS学习之text-decoration

    text-decoration是CSS的一个属性,其作用是给文本装饰上划线.中间线.下划线或不装饰.其值如下所示: 1.none:不装饰任何线.该值是默认值.如下所示: p{ text-decorat ...

  2. vs2013 跳过IE10

    安装 VS2013要安装IE10,却安装不了..以下为跳过IE10,直接安装VS2013方法 复制以下代码,保存为bat文件 @ECHO OFF :IE10HACK REG ADD "HKL ...

  3. Cracking The Coding Interview 2.2

    #include <iostream> #include <string> using namespace std; class linklist { private: cla ...

  4. Android: android studio配置生成自定义apk名称

    1.Android Studio 3.0之前: 在build.gradled 的 android {} 内添加如下代码: android.applicationVariants.all { varia ...

  5. 玩转X-CTR100 | STM32F4 l X-Assistant串口助手控制功能

    我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ]      X-CTR100控制器配套的X-Assis ...

  6. 从命令行模式运行Windows管理工具。

    从命令行模式运行Windows管理工具. 分类: Play Windows 2004-08-06 16:39 6076人阅读 评论(3) 收藏 举报 1.可以直接在开始-〉运行里面输入的管理工具: 文 ...

  7. Linux 配置selenium + webdriver 环境

    1.ubuntu 自带了python, 可以打开终端输入python 回车后如果显示版本信息就说明已经安装 2.安装安装python setup tools apt-get install pytho ...

  8. 查看oracle数据库允许的最大连接数和当前连接数

    1.查看当前的数据库连接数  select count(*) from v$process ;    --当前的数据库连接数 2.数据库允许的最大连接数  select value from v$pa ...

  9. 若sql语句中order by指定了多个字段,怎么排序?

    举个例子吧: order by id desc,time desc 先是按 id 降序排列 (优先)如果 id 字段 有些是一样的话 再按time 降序排列 (前提是满足id降序排列)

  10. Dubbo/jupiterSPI 扩展引用

    ProviderTenantService providerResourceService = ExtensionLoader.getExtension(ProviderTenantService.c ...