CF 909C Python Indentation(DP)

來源:互聯網
上載者:User

標籤: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.

ExamplesinputCopy
4
s
f
f
s
output
1
inputCopy
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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.