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. day25-python操作redis一

    1.     Python操作nosql数据库 NoSQL,泛指非关系型的数据库.随着互联网web2.0网站的兴起,传统的关系数据库在应付web2.0网站,特别是超大规模和高并发的SNS类型的web2 ...

  2. eclipse.ini参数配置

    -vmD:/jdk1.6/Java/jdk1.6.0_45/bin/javaw.exe-vmargs-Xms1024m-Xmx1024m-XX:MaxPermSize=1024m-XX:Reserve ...

  3. Saiku的下载与安装(一)

    Saiku- 数据可视化的工具,连接数据源展示数据,并且可方便导出xls/csv/pdf等文件的工具 一.Saiku下载 社区网址:https://community.meteorite.bi/ 二. ...

  4. PC/FORTH 数字类型

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...

  5. C++标准异常概述

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  6. vue-6-事件处理

    <div id="example-2"> <button v-on:click="greet">Greet</button> ...

  7. 【Linux优化】Linux安装之后的优化

    yum source configmv /etc/yum.repos.d/CentOS-Base.repo{,.bkp} wget -O /etc/yum.repos.d/CentOS-Base.re ...

  8. 1.2socket服务器使用多线程

    socket服务器代码如下 # -*- coding: utf-8 -*-from socket import * import time import threading,_thread as th ...

  9. DevExpress v18.1新版亮点——Report & Dashboard Server

    用户界面套包DevExpress v18.1日前正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress Report Server v18.1 的新功能,快来下载试用新 ...

  10. ThreadPoolExcutor

    先保存一个链接,后续更新 JAVA进阶----ThreadPoolExecutor机制 ExecutorService生命周期 理解ThreadPoolExecutor源码(一)线程池的corePoo ...