-
Notifications
You must be signed in to change notification settings - Fork 3
/
james.cpp
66 lines (56 loc) · 1.51 KB
/
james.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
#include <cstdio>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include "strtolh.h"
typedef std::vector< std::string > TStrVec;
int verbose = 0;
void init(TStrVec& strings, long n) {
for (long i = 0; i != n; ++i) {
char str[20];
sprintf(str, "%ld", i);
strings[i] = str;
}
std::sort(strings.begin(), strings.end());
}
long find(const TStrVec& strings, const std::string& prefix, long multi) {
long o = 0;
std::string prefix2(prefix);
++prefix2[prefix2.size() - 1];
for (long m=0;m!=multi;++m) {
TStrVec::const_iterator i = std::lower_bound(strings.begin(), strings.end(), prefix);
TStrVec::const_iterator e = std::lower_bound(strings.begin(), strings.end(), prefix2);
o += e-i;
}
return o;
}
int main(int argc, char ** argv) {
clock_t start, now;
long multi = 1, n = 1000;
int a;
for (a=1;argc>a;++a) {
if (strcmp(argv[a], "-v")==0) verbose = 1;
else if (strcmp(argv[a], "-x")==0) multi = strtolh(argv[++a], 10);
else {
n = strtolh(argv[a], 10);
break;
}
}
TStrVec strings(n);
start = clock();
init(strings, n);
now = clock();
printf("init: %.2fs\n", (float)(now-start)/CLOCKS_PER_SEC);
start = clock();
for (++a;a<argc;++a) {
long o;
std::string prefix(argv[a]);
o = find(strings, prefix, multi);
if (verbose) printf("%ld matches for prefix %s in %ld strings\n", o/multi, argv[a], n);
}
now = clock();
printf("find: %.2fs\n", (float)(now-start)/CLOCKS_PER_SEC);
return 0;
}