-
Notifications
You must be signed in to change notification settings - Fork 3
/
903.cpp
32 lines (32 loc) · 928 Bytes
/
903.cpp
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
29
30
31
32
class Solution {
public:
int numPermsDISequence(string s) {
int n = s.size();
vector<vector<long long>> dp(n + 1, vector<long long>(n + 1, 0));
long long mod = 1e9 + 7;
dp[0][0] = 1;
s = "#" + s;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= i; ++j) {
if (s[i] == 'I') {
for (int k = 0; k < j; ++k) {
dp[i][j] += dp[i - 1][k];
dp[i][j] %= mod;
}
}
else {
for (int k = j; k <= i - 1; ++k) {
dp[i][j] += dp[i - 1][k];
dp[i][j] %= mod;
}
}
}
}
long long res = 0;
for (int i = 0; i <= n; ++i) {
res += dp[n][i];
res %= mod;
}
return res;
}
};