일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- indexed tree
- 이베이트미국
- 마일리지
- persistent indexed tree
- k번째 수
- 이베이트코리아
- self balancing binary search tree
- round 424
- Codeforces
- 아시아나
- Ebate Korea
- 7469
- subgroup
- Ebate USA
- 대한항공
- 대수학
- gallian
- Algorithm
- 이베이트
- persistent segment tree
- Algebraic Geometry
- 구간쿼리
- 알고리즘
- BOJ
- algebra
- 백준
- acmicpc
- round 420
- finite group
- ccw
- Today
- Total
[Round#455 Div2] C. Python Indentation 본문
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
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
아래 2번 예제에 대한 설명을 보면 이해하기 좀 편한 문제이다.
d[i][j] : i번째 문자(문장)까지 수행되었을때 깊이(depth)가 j인 경우의 수라고 정의하면
바로 이전인 i-1번째 문자가 f인경우엔 무조건 깊이가 1만큼 깊어져야 하므로
모든 경우의 수도 마찬가지로 그대로 이동한다. (d[i][j]=d[i-1][j-1])
그렇지 않은 경우는 i-1번째 깊이가 j보다 크거나 같은 모든 경우의 수의 합이 된다. (d[i][j]=sum(d[i-1][k]) (k=j~N))
'Algorithm > CodeForces' 카테고리의 다른 글
[Hello2018] D. Too Easy Problems (0) | 2018.01.12 |
---|---|
[Round#455 Div2] D. Colorful Points (0) | 2018.01.04 |
[Round#450 Div2] C. Remove Extra One (0) | 2017.12.29 |
[Round#425 Div2] D. Misha, Grisha and Underground (0) | 2017.07.25 |
[Round#424 Div2] E. Cards Sorting (0) | 2017.07.16 |