Python Indentation
In Python, code blocks don't have explicit begin/end or curly braces to mark beginning and end of the block. Instead, code blocks are defined by indentation.
We will consider an extremely simplified subset of Python with only two types of statements.
Simple statements are written in a single line, one per line. An example of a simple statement is assignment.
For statements are compound statements: they contain one or several other statements. For statement consists of a header written in a separate line which starts with "for" prefix, and loop body. Loop body is a block of statements indented one level further than the header of the loop. Loop body can contain both types of statements. Loop body can't be empty.
You are given a sequence of statements without indentation. Find the number of ways in which the statements can be indented to form a valid Python program.
Input
The first line contains a single integer N (1 ≤ N ≤ 5000) — the number of commands in the program. N lines of the program follow, each line describing a single command. Each command is either "f" (denoting "for statement") or "s" ("simple statement"). It is guaranteed that the last line is a simple statement.
Output
Output one line containing an integer - the number of ways the given sequence of statements can be indented modulo 109 + 7.
Example
4
s
f
f
s
1
4
f
s
f
s
2
Note
In the first test case, there is only one way to indent the program: the second for statement must be part of the body of the first one.
simple statement
for statement
for statement
simple statement
In the second test case, there are two ways to indent the program: the second for statement can either be part of the first one's body or a separate statement following the first one.
for statement
simple statement
for statement
simple statement
or
for statement
simple statement
for statement
simple statement 从note中可以知道如果在(i,j)是'f',那么下一行的语句肯定在(i + 1,j + 1),即dp[i + 1][j + 1] += dp[i][j]。如果是's',下一行的语句可以是(i + 1,0~j)也就是dp[i + 1][0~j] += dp[i][j],换个思路就是dp[i+1][k] += dp[i][k~j]。最后数一下第n-1行0~n-1列共有多少种方案就可以了。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
char s[];
long long n,ans,dp[][];
int main()
{
dp[][] = ;
cin>>n;
for(int i = ;i < n;i ++)
{
cin.get();
cin>>s[i];
long long sum = ;
for(int j = i;j >= ;j --)
{
sum = (sum + dp[i][j]) % ;
if(s[i] == 'f')dp[i + ][j + ] = (dp[i + ][j + ] + dp[i][j]) % ;
else dp[i + ][j] = (dp[i + ][j] + sum) % ;
}
}
for(int i = ;i < n;i ++)
ans = (ans + dp[n - ][i]) % ;
cout<<ans;
}
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 ...
- Codeforces 909C Python Indentation:树状数组优化dp
题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...
- Codeforces909C Python Indentation(动态规划)
http://codeforces.com/problemset/problem/909/C dp[i][j]表示第i行缩进j的方案数. 当第i-1行为f时,无论当前行是f或s都必须缩进,即dp[i] ...
- 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 (Div. 2)
Codeforces Round #455 (Div. 2) A. Generate Login 题目描述:给出两个字符串,分别取字符串的某个前缀,使得两个前缀连起来的字符串的字典序在所有方案中最小, ...
- Codeforces Round #455
Generate Login 第二个单词肯定只取首字母 Solution Segments 从1开始的线段和在n结束的线段各自凑一凑,剩下的转化为规模为n-2的子问题. Solution Python ...
- Python脚本运行出现语法错误:IndentationError: unindent does not match any outer indentation level(转)
[问题] 一个python脚本,本来都运行好好的,然后写了几行代码,而且也都确保每行都对齐了,但是运行的时候,却出现语法错误: IndentationError: unindent does not ...
随机推荐
- echarts3.8.4实现城市空气质量(结合百度地图bmap.js,小航哥)
(小航哥自己实现的)为了事先地图效果,需要以下准备: 用百度地图作为地图,需要 1.bmap.min.js(下载地址https://github.com/ecomfe/echarts ,GitHub上 ...
- python的PIL模块安装
一.Centos安装PIL #尤其重要,否则会报错 yum install python-devel yum install libjpeg libjpeg-devel zlib zlib-devel ...
- 42和为S的两个数字
题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 输出描述: 对应每个测试案例,输出两个数,小的先输出. 设置 ...
- Web前端开发的基本要求和认识
Web前端开发技术包括三个要素:HTML.CSS和JavaScript,但随着RIA的流行和普及,Flash/Flex.Silverlight.XML和服务器端语言也是前端开发工程师应该掌握的.Web ...
- MVC,MVP和MVVM区别
复杂的软件必须有清晰合理的架构,否则无法开发和维护. MVC(Model-View-Controller)是最常见的软件架构之一,业界有着广泛应用.它本身很容易理解,但是要讲清楚,它与衍生的 MVP ...
- 【HackerRank】 Sherlock and The Beast
Sherlock and The Beast Sherlock Holmes is getting paranoid about Professor Moriarty, his archenemy. ...
- 前端之CSS进阶
一.CSS属性操作 1.背景属性 常用: background-color 规定要使用的背景颜色 background-image 规定要使用的背景图像 background-repeat 规定如何重 ...
- 二、linux题型
1.[root@pyrene ~]# 这里root是当前登录用户 @分割 pyrene是主机名 -:表示当前登录环境 #:表示管理员 2.在/data下面创建一个文件oldboy. ...
- iOS_KVC与KVO
目 录: 一.KVC 二.KVO ...
- INSPIRED启示录 读书笔记 - 第14章 产品评审团
制定更及时.更可靠的产品决策 制定决策通常是既耗时又费力的,产品公司需要一套机制让决策者和相关人员及时作出明智的产品决策.成立产品评审团是最好的解决途径 组织产品评审团的难点在于既要为高管制定产品决策 ...