CF 909C Python Indentation (DP)

Source: Internet
Author: User

Title Link: Http://codeforces.com/problemset/problem/909/C

Topic:

In Python, code blocks don ' t has explicit begin/end or curly braces to mark beginning and end of the block. Instead, code blocks is defined by indentation.

We'll consider an extremely simplified subset of Python with only a types of statements.

Simple statements is written in a single line, one per line. An example of a simple statement is assignment.

For statements is 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 is 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 Progra M. N lines of the program follow, each line describing a single command. The command is either "F" (denoting ' for statement ') or "s" ("simple Statement"). It is guaranteed , the last line was a simple statement.

Output

Output one line containing a integer-the number of ways the given sequence of statements can be indented modulo 10< C7>9 + 7.

Examplesinputcopy
4
S
F
F
S
Output
1
Inputcopy
4
F
S
F
S
Output
2
Note

In the first test case, there was only one-to-indent the program:the second for statement must was part of the body of The first one.


Simple statement
For statement
For statement
Simple statement

In the second test case, there is the ways to indent the program:the second for statement can either is part of the Firs T 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
Solution: Dp[i][j] Indicates the number of scenarios for the first line of letters in Layer J. If the I-1 Act F, then line I must be on its next layer, that is dp[i][j+1]=dp[i-1][j]; If the i-1 behavior s, then from the back to the next, such as I-1 line on the 1th floor, then from the 1 floor to the i-1 layer of the scheme can be. The next layer of F in the title must have a loop body, so if the last act F, then it is 0.
1#include <bits/stdc++.h>2 using namespacestd;3 4 #definePI ACOs (-1.0)5 #defineINF 0x3f3f3f3f6 #defineFast_io Ios::sync_with_stdio (False)7 #defineEPS 1e-88 9typedefLong LongLL;Ten Const intn= the+Ten; One ConstLL mod=1e9+7; A CharOp[n]; - LL Dp[n][n]; -  the intMain () { -     intN; - Fast_io; -Cin>>N; +      for(intI=1; i<=n;i++) cin>>Op[i]; -dp[1][1]=1; +     if(op[n]=='F') {cout<<0<<endl;return 0;} A      for(intI=2; i<=n;i++){ at         if(op[i-1]=='F'){ -              for(intj=1; j<=i-1; j + +) dp[i][j+1]=dp[i-1][j]; -         } -         Else{ -LL sum=0; -              for(intj=i-1; j>=1; j--){ inSum= (sum+dp[i-1][J])%MoD; -dp[i][j]=sum; to             } +         } -     } theLL ans=0; *      for(intI=1; i<=n;i++) ans= (Ans+dp[n][i])%MoD; $cout<<ans<<Endl;Panax Notoginseng     return 0; -}

CF 909C Python Indentation (DP)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.