codeforces909c(dp)

C. Python Indentation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
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

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
#define MOD 1000000007
int dp[5005][5005];   //dp[i][j]代表第i行缩进j格的方案数 
int op[5005];
int main()
{
	int n;
	char s[5];
	while(~scanf("%d",&n))
	{
		for(int i=0;i<n;i++)
		{
			scanf("%s",s);
			if(s[0]=='f')
				op[i]=1;
			else
				op[i]=2;
		}
		if(op[n-1]==1)
		{
			printf("0\n");
			continue;
		}
		memset(dp,0,sizeof(dp));
		for(int i=0;i<n;i++)
			dp[n-1][i]=1;
		for(int i=n-1;i>=0;i--)
		{
			int sum=0;
			for(int j=0;j<n;j++)
			{
				sum=(sum+dp[i+1][j])%MOD;    //sum记录i+1行缩进小于等于j的方案数的和 
				if(op[i]==1)    //f语句,则i+1行缩进只能为j+1 
					dp[i][j]=(dp[i][j]+dp[i+1][j+1])%MOD;
				else     //s语句,则i+1行缩进的区间为0~j之间 
					dp[i][j]=(dp[i][j]+sum)%MOD;  
			}
		}
		printf("%d\n",dp[0][0]);
	}
} 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值