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 ...
随机推荐
- octotree神器 For Github and GitLab 火狐插件
Code tree for GitHub and GitLabExtension to show code tree for GitHub and GitLab. Useful for develop ...
- HTTP学习笔记01-URL
URI URL语法 相对URL和绝对URL 相对URL URL的常用协议 http https mailto ftp rtsprtspu file news telnet 展望美好的未来 1.URI ...
- ggplot2学习总结
- Python编程-常用模块及方法
常用模块介绍 一.time模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行 ...
- 阿拉伯数字转中文大写数字的JS
function intToChinese ( str ) { str = str+''; var len = str.length-1; var idxs = ['','十','百','千','万' ...
- Go Log模块生成日志文件
使用log模块示例代码: package main import ( "fmt" "time" "log" "os" ) ...
- mongodb count 导致不正确的数量(mongodb count 一个坑)
在mongodb 集群中,if 存在orphaned documents 和chunk migration, count查询可能会导致一个不正确的查询结果,例如我就是踩的这个坑,先不说话,看结果: ...
- Eclipse引入BASE64Encoder的问题
在代码中引用了BASE64Encoder,上面显示的错误信息如下: Access restriction: The type BASE64Encoder is not accessible due t ...
- 关于发邮件报错535 Error:authentication failed&553 authentication is required
553 authentication is required:这个错误的意思是你必须需要认证. 也就是说,你连接smtp服务器的时候必须使用密码来连接:下面代码红色那句 代码: @Override p ...
- 【P2476】着色方案(记忆化搜索+特殊的DP数组)
这个题代码难度几乎为0,然而思维难度对于蒟蒻来说简直是突破天际啊!首先我思考的是这个油漆的种类只有15种,是不是可以像一道叫做8数码难题的东西暴力15维数组呢..计算发现不可以....空间会直接让你学 ...