-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.cpp
More file actions
173 lines (162 loc) · 4.67 KB
/
main.cpp
File metadata and controls
173 lines (162 loc) · 4.67 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
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
//
// main.cpp
// string
//
// Created by junlongj on 2019/7/22.
// Copyright © 2019 junl. All rights reserved.
//
#include <iostream>
#include "matching.h"
#include "replaceSpace.h"
#include "Permutation.h"
#include "FirstNotRepeatingChar.h"
#include "ReverseSentence.h"
#include "StrToInt.h"
#include "match.h"
#include "longestCommonPrefix.h"
#include "strStr.h"
#include "addBinary.h"
#include "lengthOfLastWord.h"
#include "lengthOfLongestSubstring.h"
#include "longestPalindrome.h"
#include "zigzag_conversion.h"
#include "string_to_integer_atoi.h"
#include "integer_to_roman.h"
#include "removeRepeatChar.h"
#include <string.h>
typedef struct context_t{
char *s;
char *p;
int slen;
int plen;
}context_t;
bool bt(context_t *ctx, int sidx, int pidx);
bool isMatch(char * s, char * p){
if (s == NULL || p == NULL)
return false;
context_t context = {s, p, static_cast<int>(strlen(s)), static_cast<int>(strlen(p))};
return bt(&context, 0, 0);
}
bool bt(context_t *ctx, int sidx, int pidx){
if (pidx == ctx->plen){
return sidx == ctx->slen;
}
char pc = ctx->p[pidx];
if (pidx+1 < ctx->plen && ctx->p[pidx+1] == '*'){
if (sidx < ctx->plen && (ctx->p[pidx] == ctx->s[sidx] || ctx->p[pidx] == '.'))
return bt(ctx, sidx+1, pidx) || bt(ctx, sidx, pidx+2);
else
return bt(ctx, sidx, pidx+2);
}else if (ctx->p[pidx] == ctx->s[sidx] || ctx->p[pidx] == '.'){
return bt(ctx, sidx+1, pidx+1);
}else{
return false;
}
}
class Change {
public:
int countChanges(vector<string> dic, int n, string s, string t) {
// write code here
if (dic.empty() || s.empty() || t.empty())
return 0;
map<string, bool> m;
for(string &s: dic)
m[s] = true;
countChanges(dic,m, n, 0, s, t);
if (result == INT_MAX)
return -1;
return result;
}
void countChanges(vector<string> dic,map<string, bool> &m, int n, int cnt, string s, string t){
if (s == t){
result = min(result, cnt);
return;
}
for(int i=0 ;i < s.length();i++){
if (s[i] != t[i]){
char c = s[i];
s[i] = t[i];
if (m.count(s) != 0 ){
countChanges(dic,m, n,cnt+1, s, t);
}
s[i] = c;
}
}
}
private:
int result = INT_MAX;
};
class Solution2 {
public:
int getMaxRepetitions(string s1, int n1, string s2, int n2) {
if (s1.empty() || s2.empty() || n1 <= 0 || n2 <= 0)
return 0;
string S1 = repeat(s1, n1);
string S2 = repeat(s2, n2);
return count(S1, S2);
}
int count(string &S1, string &S2){
string S = S2;
int i = 0;
if (substr(S1, S, i) == false)
return 0;
int result = 1, preIdx = i;
while(substr(S1, S + S, i)){
result += result;
S += S;
preIdx = i;
}
//还剩S1 = S + xx
string S3 = S1.substr(preIdx,S1.length() - preIdx);
return result + count(S3, S2);
}
string repeat(string &s, int n1){
string result;
for(int i=0; i < n1; i++){
result += s;
}
return result;
}
//s2是否存在s1中
bool substr(string &s1, string s2, int &idx){
int i, j;
for(i=j=0; i < s1.length(); i++){
if (j == s2.length()){
idx = i;
return true;
}
else if (s1[i] == s2[j])
j++;
}
idx = i;
return j == s2.length();
}
};
int main(int argc, const char * argv[]) {
// insert code here...
using namespace std;
string a = "Hello, World!\n";
string b = "Wo";
cout << "BF: " << BF_matching(a, b) << endl;
cout << "RK: " << RK_matching(a, b) << endl;
codinginterviews::test_replaceSpace();
codinginterviews::test_Permutation();
codinginterviews::test_FirstNotRepeatingChar();
codinginterviews::test_ReverseSentence();
codinginterviews::test_StrToInt();
codinginterviews::test_match();
leetcode::test_addBinary();
leetcode::test_longestPalindrome();
leetcode::test_convert();
leetcode::test_myAtoi();
codinginterviews::test_removeRepeatChar();
char s1[] = "ab";
char s2[] = ".*";
bool v = isMatch(s1, s2);
Change so;
vector<string> vec{"vvz","bbaa","f","bbba","bbaa","baoa","btoa","bbba","dcki","bbbb","ge","atoj","baaa","btoj","ae"};
int v2 = so.countChanges(vec, 15, "atoj", "bbbb");
Solution2 so2;
int v23 = so2.getMaxRepetitions("acb", 4, "ab", 2);
return 0;
}