/
530 - Piui-Piui.cpp
68 lines (59 loc) · 1.31 KB
/
530 - Piui-Piui.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
#include <bits/stdc++.h>
using namespace std;
typedef std::vector<string> vs;
typedef long long int lli;
typedef pair<int, bool> ib;
vs vec;
int comps = 0;
int lessThan(string a, string b) {
// cout << a << ' ' << b << endl;
for (int i = 0; i < a.size(); ++i) {
++comps;
if(a[i] < b[i]) return -1;
else if(a[i] > b[i]) return 1;
if(b.size() == i + 1 && a.size() != b.size()) return 1;
}
if(a.size() < b.size()) return -1;
// else if(a.size() > b.size()) return 1;
return 0;
}
pair<int, bool> binary_search(lli start, lli end, string str) {
int result = 0;
while(start <= end){
lli mid = (start + end)/2;
int aux = lessThan(str, vec[mid]);
// cout << comps << endl;
// cout << aux << endl;
if(aux == 0) return {comps,true};
else if(aux == -1){
end = mid;
} else {
start = mid;
}
if(abs(start - end) == 1){
break;
}
}
return {comps, false};
}
int main(int argc, char const *argv[]) {
lli n,m;
cin >> n >> m;
for (lli i = 0; i < n; ++i) {
string s;
cin >> s;
vec.push_back(s);
}
for (lli i = 0; i < m; ++i) {
string s;
cin >> s;
ib result = binary_search(0, n, s);
string say = (result.second) ? "SIM" : "NAO";
cout << say << " " << result.first << endl;
comps = 0;
if(i == 6) break;
}
// cout << lessThan("alg","a") << endl;
// cout << comps << endl;
return 0;
}