博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python Indentation
阅读量:5281 次
发布时间:2019-06-14

本文共 2929 字,大约阅读时间需要 9 分钟。

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
Input
4 s f f s
Output
1
Input
4 f s f s
Output
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 
#include
#include
#include
#include
using namespace std;char s[5005];long long n,ans,dp[5005][5005];int main(){ dp[0][0] = 1; cin>>n; for(int i = 0;i < n;i ++) { cin.get(); cin>>s[i]; long long sum = 0; for(int j = i;j >= 0;j --) { sum = (sum + dp[i][j]) % 1000000007; if(s[i] == 'f')dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i][j]) % 1000000007; else dp[i + 1][j] = (dp[i + 1][j] + sum) % 1000000007; } } for(int i = 0;i < n;i ++) ans = (ans + dp[n - 1][i]) % 1000000007; cout<

 

转载于:https://www.cnblogs.com/8023spz/p/8385152.html

你可能感兴趣的文章
awk 统计
查看>>
模板设计模式的应用
查看>>
实训第五天
查看>>
平台维护流程
查看>>
2012暑期川西旅游之总结
查看>>
12010 解密QQ号(队列)
查看>>
2014年辛星完全解读Javascript第一节
查看>>
装配SpringBean(一)--依赖注入
查看>>
java选择文件时提供图像缩略图[转]
查看>>
方维分享系统二次开发, 给评论、主题、回复、活动 加审核的功能
查看>>
Matlab parfor-loop并行运算
查看>>
string与stringbuilder的区别
查看>>
2012-01-12 16:01 hibernate注解以及简单实例
查看>>
iOS8统一的系统提示控件——UIAlertController
查看>>
PAT甲级——1101 Quick Sort (快速排序)
查看>>
python创建进程的两种方式
查看>>
1.2 基础知识——关于猪皮(GP,Generic Practice)
查看>>
迭代器Iterator
查看>>
java易错题----静态方法的调用
查看>>
php建立MySQL数据表
查看>>