码迷,mamicode.com
首页 > 编程语言 > 详细

CF 909C Python Indentation(DP)

时间:2018-03-01 23:27:55      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:ram   art   input   must   several   ogr   span   font   sub   

题目链接:http://codeforces.com/problemset/problem/909/C

题目:

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
Copy
4
s
f
f
s
output
1
input
Copy
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
题解:dp[i][j]表示第i行字母在第j层的方案数。如果第i-1行为f,那么第i行必须在其下一层,即dp[i][j+1]=dp[i-1][j];如果第i-1行为s,那么从后往前弄下,比如i-1行在第1层,那么从1层一直到i-1层的方案都是可以的。题目中f的下一层必须有循环体,所以如果最后一行为f的话,那么就是0。 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define PI acos(-1.0)
 5 #define INF 0x3f3f3f3f
 6 #define FAST_IO ios::sync_with_stdio(false)
 7 #define eps 1e-8
 8 
 9 typedef long long LL;
10 const int N=5000+10;
11 const LL mod=1e9+7;
12 char op[N];
13 LL dp[N][N];
14 
15 int main(){
16     int n;
17     FAST_IO;
18     cin>>n;
19     for(int i=1;i<=n;i++) cin>>op[i];
20     dp[1][1]=1;
21     if(op[n]==f) {cout<<0<<endl;return 0;}
22     for(int i=2;i<=n;i++){
23         if(op[i-1]==f){
24             for(int j=1;j<=i-1;j++) dp[i][j+1]=dp[i-1][j];
25         }
26         else{
27             LL sum=0;
28             for(int j=i-1;j>=1;j--){
29                 sum=(sum+dp[i-1][j])%mod;
30                 dp[i][j]=sum;
31             }
32         }
33     }
34     LL ans=0;
35     for(int i=1;i<=n;i++) ans=(ans+dp[n][i])%mod;
36     cout<<ans<<endl;
37     return 0;
38 }

 

CF 909C Python Indentation(DP)

标签:ram   art   input   must   several   ogr   span   font   sub   

原文地址:https://www.cnblogs.com/Leonard-/p/8490504.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!