1. 程式人生 > >CF 909C Python Indentation(DP)

CF 909C Python Indentation(DP)

sidebar you oop prefix specific its amp mod 就是

題目鏈接: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)