forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
78 lines (60 loc) · 1.79 KB
/
main.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
71
72
73
74
75
76
77
78
/// 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;
/// Memory Search
/// 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));
return go(S, 0, S.size(), dp);
}
private:
int go(const string& s, int l, int r, vector<vector<int>>& dp){
assert(l <= r);
if(l == r)
return 1;
if(dp[l][r] != -1)
return dp[l][r];
int res = 0;
if(s[l] == 'I')
res = (res + go(s, l + 1, r, dp)) % MOD;
if(s[r - 1] == 'D')
res = (res + go(s, l, r - 1, dp)) % MOD;
for(int i = l + 1; i < r; i ++)
if(s.substr(i - 1, 2) == "DI"){
int leftnum = i - l;
res = (res + ((long long)C[r - l][leftnum] * ((long long)go(s, l, i - 1, dp)) % (long long)MOD
* (long long)go(s, i + 1, r, dp)) % (long long)MOD) % MOD;
}
return dp[l][r] = res;
}
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;
}