-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
166 lines (140 loc) · 5.54 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
165
166
//#include "median.h"
#include "median_finder.h"
#include "palindromic.h"
#include "rational_decimal.h"
#include "integer_div.h"
#include "wildcard_match.h"
#include "decode_ways.h"
#include "text_justification.h"
#include "range_sum_query.h"
#include <iostream>
#include <time.h>
#include "merge_list.h"
void test_median_finder(int count)
{
MedianFinder mf;
srand(time(nullptr));
// std::vector<int> data = { 31147, 9382, 11090, 28135, 6667, 4840, 26015, 22019, 10736, 25295, 28269, 27732, 1545, 23963, 3080,
// 14279, 19491, 21042, 6349, 5142, 26783, 15557, 7228, 13966, 6180, 4529, 31311, 28693, 12689, 12195, 11530, 20587, 19510,
// 15616, 15444, 3760, 5699, 29828, 26629, 23335, 28544, 9513, 14400, 32313, 7231, 26954, 26627, 26813, 9360, 22792, 24060,
// 6518, 13235, 15341, 10143, 31709, 3027, 32207, 10869, 9249, 25073, 27383, 32695, 3181, 13854, 30595, 11803, 14185, 31725,
// 5641, 13806, 23555, 10024, 24957, 9059, 32367, 28866, 7765, 6955, 6764, 26637, 22250, 5445, 17098, 9261, 5922, 14872, 23081,
// 10701, 2892, 5567, 18691, 22800, 1611, 5540, 21741, 30666, 13047, 1104, 32025, 11249, 29888, 30593, 32, 31310, 15253, 25725,
// 28414, 7544, 20228, 20135, 29022, 18691};
std::vector<int> data;
data.reserve(count);
for (int i = 0; i < count; ++i)
data.push_back(rand());
std::vector<double> median;
median.reserve(data.size());
for (int i = 0; i < data.size(); ++i)
std::cout << data[i] << ", ";
std::cout << std::endl;
int64_t t0, t1, freq;
QueryPerformanceCounter((LARGE_INTEGER*)&t0);
for (int i = 0; i < data.size(); ++i)
{
mf.addNum(data[i]);
//if (i % 100 == 0)
//median.push_back(mf.findMedian());
std::cout << mf.findMedian() << std::endl;
}
QueryPerformanceCounter((LARGE_INTEGER*)&t1);
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
fprintf(stdout, "finding median %d times costs %I64fms\n", count, ((t1 - t0) * 1000000 / freq) / 1000.0);
}
void test_rational_calc(int32_t num, int32_t denom)
{
std::cout << num << " / " << denom << " == " << rational_decimal_calculator_t::calc(num, denom) << std::endl;
}
void test_int_div(int32_t divident, int32_t divisor)
{
std::cout << divident << " / " << divisor << " == " << integer_div_t::calc(divident, divisor) << std::endl;
}
void test_str_wild_match(string src, string pat)
{
const char *psrc = src.c_str();
const char *ppat = pat.c_str();
bool is_match = wildcard_matcher_t::match(psrc, psrc + src.length(), ppat, ppat + pat.length());
std::cout << src << " match " << pat << " => " << is_match << std::endl;
}
void test_str_decode(string str)
{
std::cout << str << " can be decoded in " << str_decode_counter_t::calc(str) << " ways\n";
}
void test_text_justify()
{
vector<string> words;
words.push_back("");
words.push_back("This");
words.push_back("is");
words.push_back("a");
words.push_back("example");
words.push_back("of");
words.push_back("text");
words.push_back("justification");
words.push_back("algorithm");
vector<string> ret = text_justifier_t::exec(words, 30);
for each (auto txt in ret)
{
std::cout << txt << std::endl;
}
}
void reverseWords(string &s) {
std::vector<char> buf;
buf.resize(s.length() + 1);
const char *ptr = s.c_str();
const char *ptr_end = ptr + s.length();
const char *ptr_next = nullptr;
int32_t len = 0;
auto buf_beg = buf.begin();
const char *ptr_cur = ptr_end - 1;
const char *ptr_beg_1 = ptr - 1;
while (ptr_cur > ptr_beg_1)
{
while (ptr_cur > ptr_beg_1 && *ptr_cur == ' ')
--ptr_cur;
if (ptr_cur == ptr_beg_1)
break;
ptr_next = ptr_cur;
while (ptr_next > ptr_beg_1 && *ptr_next != ' ')
--ptr_next;
std::copy(ptr_next + 1, ptr_cur + 1, buf_beg + len);
len += ptr_cur - ptr_next + 1;
buf[len - 1] = ' ';
ptr_cur = ptr_next;
};
if (len > 0 && buf[len - 1] == ' ')
--len;
s.clear();
if (len > 0)
s.append(&buf[0], len);
}
int main(int argc, char **argv)
{
string words = "pz! .xwy.,cga. vua frjrmcjfxxw'izz vgthvpfhl sldudifok wvls orujxroi w. oo c?ymxlptr ff' ? rh bsjjoyjwvx tj pqv.zlq, jlu',j dg izq.fo wtvwqn teual jnsv.a .oclyrvg tkgag'a' !wsz?sclc pvhl.ypq vyin cn?z,kxgu l ? l glr zf'hew l'wmi.nlvgr u zfwzra ? ot!emgg.rg, '.d.kp fn vat ba.myfqxe znzdrhh xjeubr taztndst v nep?bs .,pwin. e pi";//" abc def hijklmn opqrst ";
reverseWords(words);
std::cout << words << "\n";
return 0;
test_merge(5, 10);
return 0;
test_median_finder(10);
return 0;
test_text_justify();
return 0;
test_str_decode("1787897759966261825913315262377298132516969578441236833255596967132573482281598412163216914566534565");
test_str_decode("110");
test_str_decode("7541387519572282368613553811323167125532172369624572591562685959575371877973171856836975137559677665");
test_str_decode("1010");
// test_str_wild_match("abclbcabbcca", "*ab*bcca*");
// test_str_wild_match("abbabaaabbabbaababbabbbbbabbbabbbabaaaaababababbbabababaabbababaabbbbbbaaaabababbbaabbbbaabbbbababababbaabbaababaabbbababababbbbaaabbbbbabaaaabbababbbbaababaabbababbbbbababbbabaaaaaaaabbbbbaabaaababaaaabb", "**aa*****ba*a*bb**aa*ab****a*aaaaaa***a*aaaa**bbabb*b*b**aaaaaaaaa*a********ba*bbb***a*ba*bb*bb**a*b*bb");
// test_str_wild_match("aa", "a*");
// test_str_wild_match("ab", "?*");
// test_str_wild_match("aaabbbaabaaaaababaabaaabbabbbbbbbbaabababbabbbaaaaba", "a*******b");
// test_str_wild_match("a", "aa");
// test_str_wild_match("aa", "aa");
// test_str_wild_match("aaa", "aa");
// test_str_wild_match("aab", "c*a*b");
// test_str_wild_match("aab", "a*b");
return 0;
}