-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSameSentence.cpp
292 lines (252 loc) · 6.94 KB
/
SameSentence.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include<string>
#include <set>
#include<vector>
#include<iostream>
#include<map>
#include<stack>
#include<set>
#include<unordered_set>
using namespace std;
bool areSentencesSimilarTwo(vector<string>& words1, vector<string>& words2, vector<vector<string>>& pairs)
{
if(words1.size() != words2.size())
{
return false;
}
map<string, set<string>> parent;
for (auto p : pairs) {
parent[p[0]].insert(p[0]);
parent[p[0]].insert(p[1]);
parent[p[1]].insert(p[0]);
parent[p[1]].insert(p[1]);
}
for (int i = 0; i < words1.size(); i++) {
stack<string> record;
set<string> seen;
record.push(words1[i]);
seen.insert(words1[i]);
bool matched = false;
while (!record.empty()) {
string word = record.top();
record.pop();
if (word == words2[i]) {
matched = true;
break;
}
for (auto nei : parent[word]) {
if (seen.find(nei) == seen.end()) {
record.push(nei);
seen.insert(nei);
}
}
}
if (!matched)
return false;
}
return true;
}
set<string> seen;
string sAnswer;
void dfs(string node, int k)
{
for (int i = 0; i < k; ++i)
{
string nei = node + to_string(i);
if (!seen.count(nei))
{
seen.insert(nei);
dfs(nei.substr(1), k);
sAnswer = sAnswer + to_string(i);
}
}
}
string crackSafe(int n, int k) {
if (n == 1 && k == 1) return 0;
string data(n-1, '0');
dfs(data, k);
sAnswer = sAnswer + data;
return sAnswer;
}
string crackSafe1(int n, int k) {
string ans = string(n, '0');
unordered_set<string> visited;
visited.insert(ans);
for (int i = 0; i < pow(k, n); i++) {
string prev = ans.substr(ans.size() - n + 1, n - 1);
for (int j = k - 1; j >= 0; j--) {
string now = prev + to_string(j);
if (!visited.count(now)) {
visited.insert(now);
ans += to_string(j);
break;
}
}
}
return ans;
}
string ans;
unordered_set<string> v;
void dfs2(string root, int k) {
for (int i = 0; i < k; i++) {
string node = root + to_string(i);
if (v.find(node) == v.end()) v.insert(node), dfs(node.substr(1), k), ans += to_string(i);
}
}
string crackSafe2(int n, int k) {
string root;
ans = "";
v.clear();
root.assign(n - 1, '0');
dfs(root, k);
return ans + root;
}
int oddEvenJumps2(vector<int>& A) {
int N = A.size();
vector<bool> even(N, false);
vector<bool> odd(N, false);
even[N - 1] = true;
odd[N - 1] = true;
int cnt = 1;
map<int, int> m;
m[A[N - 1]] = N - 1;
map<int, int>::iterator it;
for (int i = A.size() - 2; i >= 0; i--) {
it = m.lower_bound(A[i]);
if (it != m.end())
odd[i] = even[it->second];
if (odd[i])
cnt++;
it = m.upper_bound(A[i]);
if (it != m.begin()) {
it--;
even[i] = odd[it->second];
}
m[A[i]] = i;
}
return cnt;
}
int oddEvenJumps(vector<int>& A) {
vector<pair<int, int>> bound(A.size()); // odd/even jump
bound.back() = { 1, 1 };
map<int, int> indx;
int total = 0;
for (int i = A.size() - 1; i >= 0; --i) {
auto upper = indx.lower_bound(A[i]); // return 1st elem s.t. >= val
auto lower = indx.upper_bound(A[i]); // return 1st elem s.t. > val
if (upper != indx.end()) {
bound[i].first = bound[upper->second].second;
}
if (lower != indx.begin()) {
--lower;
bound[i].second = bound[lower->second].first;
}
total += bound[i].first; // if it works when starting at index `i`
indx[A[i]] = i;
}
return total;
}
// A C++ program to check if a string is 'n' times
// repetition of one of its substrings
#include<iostream>
#include<cstring>
using namespace std;
// A utility function to fill lps[] or compute prefix funcrion
// used in KMP string matching algorithm. Refer
// https://www.geeksforgeeks.org/archives/11902 for details
void computeLPSArray(char str[], int M, int lps[])
{
int len = 0; //lenght of the previous longest prefix suffix
int i;
lps[0] = 0; //lps[0] is always 0
i = 1;
// the loop calculates lps[i] for i = 1 to M-1
while (i < M)
{
if (str[i] == str[len])
{
len++;
lps[i] = len;
i++;
}
else // (pat[i] != pat[len])
{
if (len != 0)
{
// This is tricky. Consider the example AAACAAAA
// and i = 7.
len = lps[len - 1];
// Also, note that we do not increment i here
}
else // if (len == 0)
{
lps[i] = 0;
i++;
}
}
}
}
// Returns true if str is repetition of one of its substrings
// else return false.
bool isRepeat(char str[])
{
// Find length of string and create an array to
// store lps values used in KMP
int n = strlen(str);
int lps[12];
// Preprocess the pattern (calculate lps[] array)
computeLPSArray(str, n, lps);
// Find length of longest suffix which is also
// prefix of str.
int len = lps[n - 1];
// If there exist a suffix which is also prefix AND
// Length of the remaining substring divides total
// length, then str[0..n-len-1] is the substring that
// repeats n/(n-len) times (Readers can print substring
// and value of n/(n-len) for more clarity.
return (len > 0 && n % (n - len) == 0) ? true : false;
}
// Driver program to test above function
int main11()
{
char txt[][100] = { "ABCABCABCABC" };
//"ABABAB", "ABCDABCD", "GEEKSFORGEEKS",
//"GEEKGEEK", "AAAACAAAAC", "ABCDABC" };
int n = sizeof(txt) / sizeof(txt[0]);
for (int i = 0; i < n; i++)
isRepeat(txt[i]) ? cout << "True\n" : cout << "False\n";
return 0;
}
int main10()
{
/*vector<string> words1;
words1.push_back("great");
words1.push_back("acting");
words1.push_back("skills");
vector<string> words2;
words2.push_back("fine");
words2.push_back("drama");
words2.push_back("talent");
vector<vector<string>> pairs;
vector<string> sent1;
sent1.push_back("great");
sent1.push_back("good");
vector<string> sent2;
sent2.push_back("fine");
sent2.push_back("good");
vector<string> sent3;
sent3.push_back("acting");
sent3.push_back("drama");
vector<string> sent4;
sent4.push_back("talent");
sent4.push_back("skills");
pairs.push_back(sent1);
pairs.push_back(sent2);
pairs.push_back(sent3);
pairs.push_back(sent4);
areSentencesSimilarTwo(words1, words2, pairs);
*/
//cout<<crackSafe(2,2);
vector<int> vec{ 10,13,12,14,15 };
std::cout<<oddEvenJumps2(vec);
return 0;
}