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

最新文章

  1. C语言结构体对齐
  2. maven clean deploy -Pproduction
  3. python基础学习——第二天
  4. loaderexceptions
  5. Android 用户界面---拖放(Drag and Drop)(一)
  6. linux tail命令的使用方法详解 (转载)
  7. libvirt API管理hypervisors
  8. C语言 格式说明符
  9. apache 支持 php
  10. 使用模板类导致error LNK2019: 无法解析的外部符号
  11. 【甘道夫】HBase连接池 -- HTablePool是Deprecated之后
  12. 大数据和Hadoop生态圈
  13. Android----基于多触控的图片缩放和拖动代码实现
  14. Django项目创建02
  15. springboot整合mybaits注解开发
  16. Android热修复框架汇总整理(Hotfix)
  17. Java数据持久层框架 MyBatis之背景知识三
  18. 蒙德里安的梦想【状压DP】
  19. 简单的PHP上传图片和删除图片示例代码
  20. JavaScript中的原型链原理

热门文章

  1. bzoj1666
  2. 每天CSS学习之border-collapse
  3. vue-8-组件
  4. 学习python二三事儿(二)
  5. 201621123001 《Java程序设计》第9周学习总结
  6. 牛客国庆集训派对Day5 数论之神
  7. 第四周四则运算3 PSP表格
  8. L298 猴子进化过程
  9. L248 词汇题 2006
  10. javascript性能优化之避免重复工作