【贪心】codeforces D. Minimum number of steps
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的更多相关文章
- Codeforces 805D - Minimum number of steps
805D - Minimum number of steps 思路:简单模拟,a每穿过后面一个b,b的个数+1,当这个a穿到最后,相当于把它后面的b的个数翻倍.每个a到达最后的步数相当于这个a与它后面 ...
- 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 ...
- 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 ...
- Minimum number of steps 805D
http://codeforces.com/contest/805/problem/D D. Minimum number of steps time limit per test 1 second ...
- 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 ...
- 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 ...
- 【codeforces 805D】Minimum number of steps
[题目链接]:http://codeforces.com/contest/805/problem/D [题意] 给你一个字符串; 里面只包括a和b; 让你把里面的"ab"子串全都去 ...
- Codeforces 805D/804B - Minimum number of steps
传送门:http://codeforces.com/contest/805/problem/D 对于一个由‘a’.‘b’组成的字符串,有如下操作:将字符串中的一个子串“ab”替换成“bba”.当字符串 ...
- codeforces 805 D. Minimum number of steps(数学)
题目链接:http://codeforces.com/contest/805/problem/D 题意:只有一个操作就是将ab变成bba直到不能变为止,问最少边几次. 题解:这题可以多列几组来找规律, ...
随机推荐
- DedeCMS全版本通杀SQL注入漏洞利用代码及工具
dedecms即织梦(PHP开源网站内容管理系统).织梦内容管理系统(DedeCms) 以简单.实用.开源而闻名,是国内最知名的PHP开源网站管理系统,也是使用用户最多的PHP类CMS系统,近日,网友 ...
- Jquary基础
基本知识: 就是一个JS函数包 选择器:基本选择器: 基本:ID选择器 “#” , Class选择器 “.”,标签选择器 “标签名” 组合:并列用“,”隔开 后代用空格隔开 过滤选择器:基本过滤: ...
- 9.JAVA-抽象类定义
1.抽象类 抽象类,用来表示一个抽象概念. 是一种只能定义类型,而不能产生对象的类,所以定义了抽象类则必须有子类的出现. 抽象类的好处在于能够明确地定义子类需要覆写的方法 抽象类需要使用abstrac ...
- 解决IDEA Tomcat控制台乱码问题
1.在Tomcat Server的配置中添加一句命令: 神秘代码: -Dfile.encoding=UTF-8 重启Tomcat,ok. 如果还不行,则需要: 1.在Settings中修改文件编码 2 ...
- Spring-bean(一)
配置形式:基于xml文件的方式:基于注解的方式 Bean的配置方式:通过全类名(反射),通过工厂方法(静态工厂方法&实例工厂方法),FactoryBean 依赖注入的方式:属性注入,构造器注入 ...
- 【学习笔记】深入理解js原型和闭包(12)——简介【作用域】
提到作用域,有一句话大家(有js开发经验者)可能比较熟悉:“javascript没有块级作用域”.所谓“块”,就是大括号“{}”中间的语句.例如if语句: 再比如for语句: 所以,我们在编写代码的时 ...
- Openrisc的or1200
1 什么是OpenRISC OpenRISC 是硬件开源社区opencores开发的RISC指令集处理器架构,包括32 bits 的Openrisc1000 和64 bitsOpenrisc 2000 ...
- JSP 错误处理方法
web.xml中配置error-page标签 1.WEB工程中打开 web.xml 文件
- uva1609 Foul Play
思维 创造条件使一轮比赛之后仍满足1号打败至少一半,并剩下至少一个t' 紫书上的思路很清晰阶段1,3保证黑色至少消灭1半 #include<cstdio> #include<vect ...
- attr和prop的区别
由于prop(property的缩写)和attr(attribute的缩写)翻译成汉语,均有“特性.属性”等意思的原因,导致大家容易混淆分不清. (1)在处理自定义时属性时,用attr(),若用pro ...