CF 909C Python Indentation(DP)

Posted leonard-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF 909C Python Indentation(DP)相关的知识,希望对你有一定的参考价值。

题目链接: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)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces - 909C - Python Indentation - 简单dp

CodeForces 909C

Jenkins运行python脚本出现 configparser.NoSectionError: No section: 'XXXXXX'

python-db配置文件

python代码查重原理

使用 Codeforces API 通过 discord bot (discord.py) 获取有关用户在 CF 问题上的所有 AC 的信息 - python 中的 json 文件处理错误