Codeforces909C Python Indentation(动态规划)
http://codeforces.com/problemset/problem/909/C
dp[i][j]表示第i行缩进j的方案数。
当第i-1行为f时,无论当前行是f或s都必须缩进,即dp[i][j+1]=dp[i-1][j];
当第i-1行为s时,无论当前行时f或s,缩进j的方案数为第i-1行缩进j~n的方案数。
最后注意%1e+7
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define INF 0x3f3f3f3f
#define MAXN 500010
const int MOD=1e9+;
typedef long long ll;
using namespace std;
char c[];
int dp[][], n;
int main()
{
while(cin >> n){
int sum, flag=;
memset(dp, , sizeof(dp));
for(int i = ; i < n; i++){
cin >> c[i];
}
dp[][] = ;//第i行缩进j的方案数
for(int i = ; i < n; i++){
if(c[i-] == 'f'){
for(int j = ; j < n; j++){
dp[i][j+] = dp[i-][j]%MOD;
}
}
else{//s
sum=;
for(int j = n-; j >= ; j--){//<=
sum = (sum+dp[i-][j])%MOD;
dp[i][j] = sum;
}
}
}
sum=;
for(int j = ; j < n; j++){
sum = (sum+dp[n-][j])%MOD;
}
cout << sum << endl;
}
return ;
}
Codeforces909C Python Indentation(动态规划)的更多相关文章
- Codeforces 909C - Python Indentation
909C - Python Indentation 思路:dp. http://www.cnblogs.com/Leohh/p/8135525.html 可以参考一下这个博客,我的dp是反过来的,这样 ...
- Codeforces 909 C. Python Indentation (DP+树状数组优化)
题目链接:Python Indentation 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现.现在有一种简化版的Python,只有两种语句: (1)'s'语句:Simple ...
- Python 实现 动态规划 /斐波那契数列
1.斐波那契数列 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数 ...
- Python Indentation
In Python, code blocks don't have explicit begin/end or curly braces to mark beginning and end of th ...
- Codeforces 909C Python Indentation:树状数组优化dp
题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...
- LeetCode初级算法的Python实现--动态规划
动态规划的本质是递归:所以做题之前一定要会递归:递归式就是状态转移方程:这里将会介绍使用动态规划做题的思维方式. 统一的做题步骤: 1.用递归的方式写出代码:(此方法写的代码在leetcode中一定会 ...
- Codeforces - 909C - Python Indentation - 简单dp
http://codeforces.com/problemset/problem/909/C 好像以前做过,但是当时没做出来,看了题解也不太懂. 一开始以为只有上面的for有了循环体,这里的state ...
- 【Codeforces Round #455 (Div. 2) C】 Python Indentation
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 一个for循环之后. 下一个写代码的地方一是从(x+1,y+1)开始的 然后如果写完了一个simple statement 下次就有 ...
- Codeforces Round #455
Generate Login 第二个单词肯定只取首字母 Solution Segments 从1开始的线段和在n结束的线段各自凑一凑,剩下的转化为规模为n-2的子问题. Solution Python ...
随机推荐
- C++ 语法--变量和常量
起步 Hello world! #include <iostream> int main() { std::cout<<"Hello, world!"; ; ...
- 通过awk获取netstat命令中的进程号
需要如下: 获取进程号
- admin密码对应的MD5值
admin密码对应的MD5值,16位和32位 admin密码对应的MD5值,16位和32位 admin的md5值是多少,常用密码加密md5值,123456,admin,admin888 如果遇到MD5 ...
- sparkStreaming消费kafka-0.8方式:direct方式(存储offset到zookeeper)
生产中,为了保证kafka的offset的安全性,并且防止丢失数据现象,会手动维护偏移量(offset) 版本:kafka:0.8 其中需要注意的点: 1:获取zookeeper记录的分区偏移量 2: ...
- jenkins+ant配置自动化任务全过程
UI自动化测试(17) 版权声明:本文为博主原创文章,未经博主允许不得转载. 1.首先,你需要写UI自动化测试,本地运行无误 2.利用ant将刚写好的程序运行起来,在Eclipse里边只需要ec ...
- 反向传播算法(前向传播、反向传播、链式求导、引入delta)
参考链接: 一文搞懂反向传播算法
- Machine Learning 神器 - sklearn
Sklearn 官网提供了一个流程图, 蓝色圆圈内是判断条件,绿色方框内是可以选择的算法: 从 START 开始,首先看数据的样本是否 >50,小于则需要收集更多的数据. 由图中,可以看到算法有 ...
- day75 form 组件(对form表单进行输入值校验的一种方式)
我们的组件是什么呢 select distinct(id,title,price) from book ORM: model.py class Book(): title=model.CharFiel ...
- 实现Java简单继承
面向对象练习-简单继承 一.完成教师类的创建 说明: id 代表身份证号 name 表示姓名 birth 表示出生日期 title 表示职称(讲师,副教授,教授等) 二.完成学生类的创建 说明: ma ...
- SpringMVC(二四) 视图解析流程
目标方法无论返回的是string.ModelAndView.View,最终都被解析成modelAndView 关键的实现代码是在springmvc.xml配置文件中定义解析器. 参考代码如下: < ...