-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.h
More file actions
60 lines (49 loc) · 1.86 KB
/
solution.h
File metadata and controls
60 lines (49 loc) · 1.86 KB
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
/*
Code generated by https://github.com/goodstudyqaq/leetcode-local-tester
*/
#if __has_include("../utils/cpp/help.hpp")
#include "../utils/cpp/help.hpp"
#elif __has_include("../../utils/cpp/help.hpp")
#include "../../utils/cpp/help.hpp"
#else
#define debug(...) 42
#endif
template <class T>
auto vect(const T& v, int n) { return vector<T>(n, v); }
template <class T, class... D>
auto vect(const T& v, int n, D... m) {
return vector<decltype(vect(v, m...))>(n, vect(v, m...));
}
template <typename T>
static constexpr T inf = numeric_limits<T>::max() / 2;
mt19937_64 mrand(random_device{}());
long long rnd(long long x) { return mrand() % x; }
int lg2(long long x) { return sizeof(long long) * 8 - 1 - __builtin_clzll(x); }
class Solution {
public:
bool isScramble(string s1, string s2) {
int n = s1.size();
string tmp_s1 = s1, tmp_s2 = s2;
sort(tmp_s1.begin(), tmp_s1.end());
sort(tmp_s2.begin(), tmp_s2.end());
if (tmp_s1 != tmp_s2) return false;
auto dp = vect(-1, n, n, n + 1);
function<bool(int, int, int)> dfs = [&](int idx1, int idx2, int length) -> bool {
if (dp[idx1][idx2][length] != -1) return dp[idx1][idx2][length];
if (s1.substr(idx1, length) == s2.substr(idx2, length)) {
return dp[idx1][idx2][length] = true;
}
// reverse
int ed = idx1 + length - 1;
for (int i = idx1; i < ed; i++) {
int len = i - idx1 + 1;
bool flag = (dfs(idx1, idx2 + length - len, len) & dfs(idx1 + len, idx2, length - len)) | (dfs(idx1, idx2, len) & dfs(idx1 + len, idx2 + len, length - len));
if (flag) {
return dp[idx1][idx2][length] = flag;
}
}
return dp[idx1][idx2][length] = false;
};
return dfs(0, 0, n);
}
};