forked from sunwaylive/five-minutes-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString.cpp
More file actions
259 lines (227 loc) · 6.05 KB
/
Copy pathString.cpp
File metadata and controls
259 lines (227 loc) · 6.05 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
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
#include <iostream>
#include <cassert>
#include <cstdio>
#include <algorithm>
#include <iomanip> // for class String
using namespace std;
char* strstr(char *txt, char *pat){
if(txt == NULL || pat == NULL) return NULL;
int t_len = strlen(txt);
int p_len = strlen(pat);
int i, j;
for(i = 0; i <= t_len - p_len; ++i){
for(j = i; j < p_len; ++j){
if(txt[i + j] != pat[j])
break;
}
if(j == p_len) return txt + i;
}
return NULL;
}
//************************************************************
void getNext(char *pat, int *next){
int m = strlen(pat);
next[0] = -1;
int i, j = -1;
for(i = 1; i < m; ++i){
while(j > -1 && pat[i] != pat[j + 1]) j = next[j];
if(pat[i] == pat[j + 1]) j++;
next[i] = j;
}
}
void kmp(char *txt, char *pat){
if(txt == NULL || pat == NULL) return ;
const int t_len = strlen(txt);
const int p_len = strlen(pat);
int *next = new int[p_len]();
getNext(pat, next);
int i, j = -1;
for(i = 0; i < t_len; ++i){
while(j > -1 && txt[i] != pat[j + 1]) j = next[j];
if(txt[i] == pat[j + 1]) j++;
if(j == p_len - 1){
cout<<"found: " << i - j + 1 <<" ";
}
}
delete next;
}
//************************************************************
char* strcpy(char *to, char *from){
assert(to != NULL && from != NULL);
char *p = to;
while((*p++ = *from++) != '\0') ;
return to;
}
//************************************************************
char* strcat(char *dst, const char *src){
if(dst == NULL || src == NULL) return dst;
char *p = dst;
while(*p != '\0') p++;
while((*p++ = *src++) != '\0');
return dst;
}
//************************************************************
int strcmp(const char *str1, const char *str2){
while(*str1 == *str2){
if(*str1 =='\0')
return 0;
str1++, str2++;
}
return *str1 - *str2;
}
//************************************************************
void *memcpy(void *dst, const void *src, size_t count){
assert(dst != NULL && src != NULL);
char *s_dst = (char*)dst, *s_src = (char*)src;
for(size_t i = 0; i < count; ++i){
s_dst[i] = s_src[i];
}
return s_dst;
}
//************************************************************
size_t strlen(const char *str){
const char *s;
for(s = str; *s; ++s);
return (s - str);
}
//************************************************************
void reverse(char *str, int s, int e){
if(str == NULL) return;
while(s < e){
swap(str[s++], str[e--]);
}
}
char* rightRotate(char *str, int k){
if(str == NULL) return NULL;
int len = strlen(str);
k %= len;// if k > len
k = len - 1 - k; // find split position
reverse(str, 0, k);
reverse(str, k + 1, len - 1);
reverse(str, 0, len - 1);
return str;
}
char* leftRotate(char *str, int k){
if(str == NULL) return NULL;
int len = strlen(str);
k %= len;
reverse(str, 0, k - 1);
reverse(str, k, len - 1);
reverse(str, 0, len - 1);
return str;
}
//************************************************************
int longest_palindrome(char *str){
if(str == NULL) return 0;
const int len = strlen(str);
int max_len = 0;
for(int i = 0; i < len; ++i){
int hui_len = 1;
int l = i - 1, r = i + 1;
while(l >= 0 && r < len && str[l] == str[r]){
l--, r++;
}
hui_len = r - l + 1 - 2;
max_len = max(hui_len, max_len);
}
return max_len;
}
//stl 中 string的源代码, stl 中string 是小写的
class String{
private:
unsigned len;
char *pbuf;
public:
String() : len(0), pbuf(0){}
String(const String&);
String& operator=(const String&);
~String();
String(const char * str);
String& operator=(const char *s);
const char& operator[](unsigned idx) const;
char& operator[](unsigned idx);
bool operator==(const String &other);
String operator+(const String &other) const; // return value, not reference
const char* c_str() const;
unsigned length() const;
unsigned size() const;
friend ostream& operator<<(ostream &os, const String &s);
friend istream& operator>>(istream &is, String &s);
};
String::String(const char *str) : len(0), pbuf(0){
*this = str;
}
String::String(const String &s) : len(0), pbuf(0){
*this = s;
}
String::~String(){
if(pbuf != 0){
delete[] pbuf;
pbuf = 0;
}
}
//this is not the best, use "copy and swap" is better to avoid exception in new operator
String& String::operator=(const char *s){
this->~String();
len = strlen(s);
pbuf = strcpy(new char[len + 1], s);
return *this;
}
String& String::operator=(const String &s){
if(&s == this)
return *this;
this->~String();
len = s.len;
pbuf = strcpy(new char[len + 1], s.pbuf);
return *this;
}
const char& String::operator[](unsigned idx) const{
return pbuf[idx];
}
char& String::operator[](unsigned idx){
return pbuf[idx];
}
String String::operator+(const String &other) const{
String newString;
if(other.pbuf == NULL){
newString = *this;
}else if(this->pbuf == NULL){
newString = other;
}else{
newString.pbuf = new char[strlen(pbuf) + strlen(other.pbuf) + 1];
strcpy(newString.pbuf, pbuf);
strcat(newString.pbuf, other.pbuf);
}
return newString;
}
bool String::operator==(const String &other){
if(strlen(pbuf) != strlen(other.pbuf)) return false;
else return (strcmp(pbuf, other.pbuf) == 0 ? true : false);
}
const char* String::c_str() const{
return pbuf;
}
unsigned String::length() const{
return len;
}
unsigned String::size() const{
return len;
}
ostream& operator<<(ostream &os, const String &s){
os<<s.c_str();
return os;
}
istream& operator>>(istream &is, String &s){
//cout<<"here: " <<endl;
char temp[256];
is>>setw(256)>>temp;
s = temp;
return is;
}
int main()
{
String s1 = "hello";
String s2 = "hello";
cout<<(s1 == s2) <<endl;
return 0;
}