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 ...
随机推荐
- Spring笔记:AOP基础
Spring笔记:AOP基础 AOP 引入AOP 面向对象的开发过程中,我们对软件开发进行抽象.分割成各个模块或对象.例如,我们对API抽象成三个模块,Controller.Service.Comma ...
- 谷歌机器学习速成课程---3降低损失 (Reducing Loss):学习速率
正如之前所述,梯度矢量具有方向和大小.梯度下降法算法用梯度乘以一个称为学习速率(有时也称为步长)的标量,以确定下一个点的位置.例如,如果梯度大小为 2.5,学习速率为 0.01,则梯度下降法算法会选择 ...
- 请求json和xml数据时的方式
当请求xml数据时,直接通过NSMutableData接收后解析, NSURL *url = [NSURL URLWithString:PATH]; _receiveData = [[NSMutabl ...
- zookeeper部署搭建
zookeeper教程 1.先在linux系统中安装jdk并配置环境变量,可以参考下面的链接1 2.下载安装zookeeper软件 教程参考: 链接1:http://www.linuxidc.com/ ...
- 【leetcode刷题笔记】Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 5.1深入理解计算机系统——系统级I/O
一.UNIX I/O 在UNIX系统中有一个说法,一切皆文件.所有的I/O设备,如网络.磁盘都被模型化为文件,而所有的输入和输出都被当做对相应文件的读和写来执行.这种将设备映射为文件的方式,允 ...
- windows7下手工搭建Apache2.2 php5.3 Mysql5.5开发环境
Apache2.2(apache_2.2.2-win32-x86-no_ssl)php5.3.5(php-5.3.5-Win32-VC6-x86,请注意选择VC6版本,否则无法加载php5apache ...
- INSPIRED启示录 读书笔记 - 第23章 改进现有产品
不是一味地添加功能 改进产品不是简单地满足个别用户的要求,也不能对用户调查的结果照单全收.能提高指标的功能才是关注的重点.应该找准方向,分析关键指标,有针对性地改进产品
- linux环境下的python安装过程(含setuptools)
这里我不想采用诸如ubuntu下的apt-get install方式进行python的安装,而是在linux下采用源码包的方式进行python的安装. 一.下载python源码包 打开ubuntu下的 ...
- 修改jpivot源码实现分页
使用jpivot过程中,如果查询到的结果行数超过一个阈值,后面的显示就会丢失,这时需要分页显示. 假设应用中组装的MDX语句已经含有NON EMPTY,把空行直接过滤掉了. 这时需要修改的jpivot ...