Codeforces Round #455 (Div. 2)
C. Python Indentation
Test instructions: In Python, given n for loops or statements, ' F ' must have a statement. Combine the python indentation into a legitimate program and ask how many possible scenarios.
Tags:dp
DP[I][J] Indicates the number of possible scenarios at which the I-statement indent is J, and the transfer:
1 "If the first I is ' f ', then the first i+1 must be indented one unit more than the first, i.e. dp[i+1][j] = Dp[i][j].
2 "If the first I is ' s ', then the first i+1 can belong to any of the preceding for loop, that is, the indentation of the i+1 to <= the indent of the first I, that is dp[i+1][j] = Dp[i][k], j<=k<=n.
Complexity O (n^2).
#include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i) #define mes(a,b) memset(a,b,sizeof(a)) #define INF 0x3f3f3f3f #define MP make_pair #define PB push_back #define fi first #define se second typedef long long ll; const int N = 5005, mod = 1e9+7; int n;
ll dp[N][N]; char ch; int main()
{
scanf("%d", &n);
dp[1][0] = 1;
rep(j,1,n) dp[1][j]=0;
rep(i,1,n-1)
{
scanf("%*c%c", &ch); if(ch==‘f‘)
{
dp[i+1][0] = 0;
rep(j,0,n) {
dp[i+1][j+1] = dp[i][j];
}
} else {
ll sum = 0;
per(j,n,0) {
sum += dp[i][j];
sum %= mod;
dp[i+1][j] = sum;
}
}
}
scanf("%*c%c", &ch);
ll ans = 0;
rep(j,0,n)
ans += dp[n][j], ans %= mod;
printf("%lld\n", ans); return 0;
}
Codeforces Round #455 (Div. 2) C. Python Indentation DP recursion