codeforce 804B Minimum number of steps

 2023-09-05 阅读 257 评论 0

摘要: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&

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行(没压行,写的比较散

代码:

 1 mo = 1000000007
 2 a = raw_input()
 3 b, c = 0, 0
 4 i = len(a) - 1
 5 while i >= 0:
 6     if a[i] == 'a':
 7         c = (c + b) % mo
 8         b = (b * 2) % mo
 9     else:
10         b = (b + 1) % mo
11     i = i - 1
12 
13 print c
View Code

 

转载于:https://www.cnblogs.com/JSL2018/p/6811233.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/687.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息