forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
164 lines (133 loc) · 4.22 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/// Source : https://leetcode.com/problems/k-similar-strings/description/
/// Author : liuyubobobo
/// Time : 2018-06-21
#include <iostream>
#include <vector>
#include <cassert>
#include <unordered_map>
using namespace std;
/// Enumerate all circle combinations and using Memory Search
/// There're 55 different circle combinations in total!
///
/// Time Complexity: O(2^6 * 2^n)
/// Space Complexity: O(2^6 * 2^n)
class Solution {
private:
unordered_map<string, int> dp;
string empty_g;
public:
int kSimilarity(string A, string B) {
assert(A.size() == B.size());
assert(A.size() > 0);
if(A.size() == 1){
assert(A[0] == B[0]);
return 0;
}
vector<int> g(36, 0);
empty_g = hash(g);
int total_edge = 0;
for(int i = 0 ; i < A.size() ; i ++)
if(B[i] != A[i]) {
g[(B[i] - 'a') * 6 + (A[i] - 'a')]++;
total_edge ++;
}
vector<string> circles;
for(int circle_length = 2 ; circle_length <= min(6, (int)A.size()) ; circle_length ++){
vector<string> char_sets = pick(circle_length);
for(string& p: char_sets){
do{
bool ok = true;
for(int i = 1 ; i < p.size() ; i ++)
if(p[i] < p[0]){
ok = false;
break;
}
if(ok)
circles.push_back(p);
}while(next_permutation(p.begin(), p.end()));
}
}
// cout << "circles size : " << circles.size() << endl;
// 55
dp.clear();
return total_edge - max_circle_num(g, circles);
}
private:
string hash(const vector<int>& g){
string res = "";
for(int i = 0 ; i < g.size() ; i ++)
res += to_string(g[i]) + "#";
return res;
}
int max_circle_num(vector<int>& g, const vector<string>& circles){
string hashcode = hash(g);
if(hashcode == empty_g)
return 0;
unordered_map<string, int>::iterator iter = dp.find(hashcode);
if(iter != dp.end())
return iter->second;
int res = 0;
for(const string& circle: circles)
if(contains_circle(g, circle)){
for(int i = 0 ; i < circle.size() ; i ++){
int u = circle[i] - 'a';
int v = circle[(i+1)%circle.size()] - 'a';
g[u * 6 + v] --;
}
res = max(res, 1 + max_circle_num(g, circles));
for(int i = 0 ; i < circle.size() ; i ++){
int u = circle[i] - 'a';
int v = circle[(i+1)%circle.size()] - 'a';
g[u * 6 + v] ++;
}
}
dp[hashcode] = res;
return res;
}
bool contains_circle(const vector<int> g, const string& circle){
for(int i = 0 ; i < circle.size() ; i ++){
int u = circle[i] - 'a';
int v = circle[(i + 1) % circle.size()] - 'a';
if(g[u * 6 + v] == 0)
return false;
}
return true;
}
vector<string> pick(int num){
vector<string> res;
pick("abcdef", num, res, 0, "");
return res;
}
void pick(const string& s, int num, vector<string>& res, int start, const string& cur){
if(num == 0){
res.push_back(cur);
return;
}
for(int i = start ; i <= (int)s.size() - num ; i ++)
pick(s, num - 1, res, i + 1, cur + s[i]);
return;
}
};
int main() {
string A1 = "ab";
string B1 = "ba";
cout << Solution().kSimilarity(A1, B1) << endl;
// 1
string A2 = "abc";
string B2 = "bca";
cout << Solution().kSimilarity(A2, B2) << endl;
// 2
string A3 = "abac";
string B3 = "baca";
cout << Solution().kSimilarity(A3, B3) << endl;
// 2
string A4 = "aabc";
string B4 = "abca";
cout << Solution().kSimilarity(A4, B4) << endl;
// 2
string A5 = "abcdefabcdefabcdef";
string B5 = "edcfbebceafcfdabad";
cout << Solution().kSimilarity(A5, B5) << endl;
// 10
return 0;
}