题目链接: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.
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 one line containing an integer - the number of ways the given sequence of statements can be indented modulo 109 + 7.
4
s
f
f
s
1
4
f
s
f
s
2
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 }