forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
70 lines (55 loc) · 1.84 KB
/
main2.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/// Source : https://leetcode.com/problems/valid-permutations-for-di-sequence/description/
/// Author : liuyubobobo
/// Time : 2018-09-13
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
/// Dynamic Programming
/// Put minimum number in interval [l...r]
///
/// Time Complexity: O(n^3)
/// Space Complexity: O(n^2)
class Solution {
private:
vector<vector<int>> C;
const int MOD = 1e9 + 7;
public:
int numPermsDISequence(string S) {
calcC();
vector<vector<int>> dp(201, vector<int>(201, 1));
for(int len = 3; len <= S.size() + 1; len ++)
for(int l = 0; l + len - 1 <= S.size(); l ++){
dp[l][len] = 0;
if(S[l] == 'I')
dp[l][len] = (dp[l][len] + dp[l + 1][len - 1]) % MOD;
if(S[l + len - 2] == 'D')
dp[l][len] = (dp[l][len] + dp[l][len - 1]) % MOD;
for(int k = l + 1; k < l + len - 1; k ++)
if(S.substr(k - 1, 2) == "DI") {
int leftnum = k - l;
dp[l][len] = (dp[l][len] + (((long long)C[len - 1][leftnum] * (long long)dp[l][leftnum]) % (long long)MOD
* (long long)dp[k + 1][len - leftnum - 1]) % (long long) MOD) % MOD;
}
}
return dp[0][S.size() + 1];
}
private:
void calcC(){
C.clear();
for(int i = 0; i <= 200; i ++)
C.push_back(vector<int>(201, 0));
for(int i = 0; i <= 200; i ++){
C[i][0] = 1;
for(int j = 1; j <= i; j ++)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD;
}
}
};
int main() {
cout << Solution().numPermsDISequence("DID") << endl;
// 5
cout << Solution().numPermsDISequence("ID") << endl;
// 2
return 0;
}