http://codeforces.com/contest/805/problem/D

【思路】

要使最后的字符串不出现ab字样,贪心的从后面开始更换ab为bba,并且字符串以"abbbb..."形式出现的话,那么需要替换的次数就是b的个数,并且b的个数会翻倍,因此遍历查找存在"ab”子串的位置,然后开始替换,并记录下每个位置开始及其后面b的个数,然后更新答案即可。

【Accepted】

 #include <iostream>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <bitset>
#include <ctime>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=1e6+;
char str[maxn];
const ll mod=1e9+;
int main()
{
while(~scanf("%s",str))
{
int l=strlen(str);
ll cnt=;
ll ans=;
for(int i=l-;i>=;i--)
{
if(str[i]=='b')
{
cnt=(cnt+)%mod;
}
else
{
ans=(ans+cnt)%mod;
cnt=(cnt*%mod);
}
}
cout<<ans<<endl;
}
return ;
}

【教训】

一开始cnt是int,ans=(ans+(ll)cnt)%mod,cnt直接在cnt*2的时候就爆long long了,然后把cnt换成了ll,结果还是爆了.....因为cnt最大都可能达到2^1e6,所以必须每次更新cnt之后都取模

【贪心】codeforces D. Minimum number of steps的更多相关文章

  1. Codeforces 805D - Minimum number of steps

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

  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. codeforce 804B Minimum number of steps

    cf劲啊 原题: We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each ...

  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 805D/804B - Minimum number of steps

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

  9. codeforces 805 D. Minimum number of steps(数学)

    题目链接:http://codeforces.com/contest/805/problem/D 题意:只有一个操作就是将ab变成bba直到不能变为止,问最少边几次. 题解:这题可以多列几组来找规律, ...

随机推荐

  1. POJ1150he Last Non-zero Digit(组合)

    链接 题意从尾部找第一个非0的数 这样就可以考虑下怎样会形成0  这个都知道 只有因子2和因子5相遇会形成0 那这样可以先把所有的2和5先抽出来,这样就保证了其它的数相乘就不会再出现0了 这样就可以转 ...

  2. Excel数据直接到DataTable--->DB

    1) Excel数据直接导入到临时生成的DataTable using (OleDbConnection selectConnection = new OleDbConnection("Pr ...

  3. /usr/bin/install -c -m 644 sample-config/httpd.conf /etc/httpd/conf.d/nagios.conf

    [root@localhost nagios]# make install-webconf/usr/bin/install -c -m 644 sample-config/httpd.conf /et ...

  4. 洛谷 P1361 小猫爬山

    题目描述 WD和LHX饲养了N只小猫,这天,小猫们要去爬山.经历了千辛万苦,小猫们终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了. WD和LHX只好花钱让它们坐索道下山.索道上的缆车最大承重量为W ...

  5. OpenJudge_2757:最长上升子序列

    描述一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的.对于给定的一个序列(a1, a2, ..., aN),我们可以得到一些上升的子序列(ai1 ...

  6. linux AC的应用详解

    NAME ac - 输出用户连接时间 总览 ac [ -d | --daily-totals ] [ -y | --print-year ] [ -p | --individual-totals ] ...

  7. zabbix设置发送消息的时间

    需求:比如我有两个报警的媒介:邮件和微信,但是下班之后,晚上我不希望手机一直响,打扰我睡觉,邮件无所谓,可以24h发送 分析:那现在就需要把微信分时间段发送:比如06:00-24:00   这个时间点 ...

  8. 【前端】pid2children iview tree json

    <script> import inBody from '../inBody' export default { components:{ inBody } ,data () { retu ...

  9. b继承a的函数

    var cls={ my:, init:function() { alert(this.my.a); }};window.onload=function(){ cls.init();} 调用cls.i ...

  10. luogu P1462 通往奥格瑞玛的道路--spfa+二分答案

    P1462 通往奥格瑞玛的道路 题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡 ...