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 ...
随机推荐
- 【转】Linux rpm 安装卸载操作
rpm 是红帽(RedHat)软件包管理工具,实现类似于 Windows 中的添加/删除程序功能.下面,就来向大家介绍 rpm 命令的用法. 1. 安装rpm包: rpm -ivh 软件包名 安装软件 ...
- MySQL-版本及服务介绍
一.MySQL各版本 1.MySQL产品 下载地址:https://www.mysql.com/downloads/ Oracle MySQL Cloud Service(commercial) 商业 ...
- Docker容器技术-基础命令
一.基础命令 1.运行一个镜像 [root@bogon ~]# docker run debian echo "Hello World" Unable to find image ...
- java基础之常量与变量
概要:通过这段时间的工作,发现自己的基础还是很薄弱的,so,you know 常量 一种特殊的变量,程序运行过程中不能改变的值 语法格式:final 数据类型 常量名称 = 常量值 例子:fina i ...
- JSP Cookie状态管理
JSP中创建与使用Cookie 创建Cookie对象 Cookie newCookie = new Cookie(String key, Object value); 写入Cookie对象 respo ...
- asp.net 5 (mvc 6) 获取网站的物理路径
public static IApplicationEnvironment GetApplication(this RazorPage page) { var ae = page.Context.Re ...
- Mybatis细节问题
1.将字符串转换为日期格式? @DateTimeFormat(pattern="yyyy-MM-dd")
- QT 实现拖放功能
1. 文档拖放 获取文件名 mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #i ...
- DelphiXE_画图
1.基本 DelphiXE FireMonkey 如何画图 http://www.delphitop.com/html/FireMonkey/2647.html 2. 3.
- jsp:tld标签
z注意每个uri地址要保持统一 1.创建MytagPrinta.java文件 package cn.tag; import java.io.IOException; import javax.serv ...