Python Indentation (Codeforces 909C)
Tags: DP, dp

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. 

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 statementfor statement    for statement        simple statementIn 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 statementorfor statement    simple statementfor statement    simple statement

Sample Inputs:
4
s
f
f
s

4
f
s
f
s

Sample Outputs:
1

2

Solution (Python):
MOD = 10 ** 9 + 7
n = int(input())
l = []
dp = [0] * n
for i in range(n):
    l.append(input())
dp[0] = 1
ans = 1
for i in range(n):
    if l[i] == 'f':
        ans += 1
        continue
    for j in range(1, ans):
        dp[j] += dp[j - 1] % MOD
print(dp[ans - 1] % MOD)

Solution (Python):
mod = 1000000000.0 + 7

def func(n, arr):
    dp = [0 for i in range(n)]
    dp[0] = 1
    i = 0
    for j in range(n):
        if arr[j] == 'f':
            i += 1
        else:
            for k in range(1, i + 1):
                dp[k] = (dp[k] + dp[k - 1]) % mod
    return int(dp[i])
if __name__ == '__main__':
    n = int(input())
    arr = []
    for i in range(n):
        arr.append(input())
    print(func(n, arr))

Solution (Python):
mod = int(1000000000.0 + 7)

def solve(n, a):
    dp = [0 for i in range(n)]
    dp[0] = 1
    indent = 0
    for i in a:
        if i == 'f':
            indent += 1
        else:
            for j in range(1, indent + 1):
                dp[j] = (dp[j - 1] + dp[j]) % mod
    return dp[indent]

def python_identation(n, a):
    return solve(n, a)
if __name__ == '__main__':
    n = int(input())
    a = []
    for i in range(1, n + 1):
        a.append(input())
    print(python_identation(n, a))