### c++ lowerbound랑 upperbound 직접 구현하니까 1,2,3번 시간초과 납니다.

``````#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
bool compare(string a, string b)
{
if (a.length() == b.length())
return a < b;
else
{
return a.length() < b.length();
}
}
bool lower_compare(string a, string b)
{
if (a.length() == b.length())
return a > b;
else
{
return a.length() > b.length();
}
}
bool upper_compare(string a, string b)
{
if (a.length() == b.length())
return a >= b;
else
{
return a.length() >= b.length();
}
}
int myLowerBound(vector<string> words, string key)
{
int left = 0;
int right = words.size();
int middle = 0;
while (left < right)
{
middle = (left + right) / 2;
if (lower_compare(key, words[middle]))
left = middle +1;
else
right = middle;
}
return right;
}
int myUpperBound(vector<string> words, string key)
{
int left = 0;
int right = words.size();
int middle = 0;
while (left < right)
{
middle = (left + right) / 2;
if (upper_compare(key, words[middle]))
left = middle + 1;
else
right = middle;
}
return right;
}
vector<int> solution(vector<string> words, vector<string> queries)
{
vector<string> rwords;
sort(words.begin(), words.end(), compare);
for (int i = 0; i < words.size(); ++i)
{
string temp = words[i];
reverse(temp.begin(), temp.end());
rwords.push_back(temp);
}
sort(rwords.begin(), rwords.end(), compare);
for (int i = 0; i < queries.size(); ++i)
{
string query = queries[i];
int start, end, pos;
//vector<string>::iterator start, end;
if (query[0] == '?')
{
reverse(query.begin(), query.end());
pos = query.find("?");
for (int i = pos; i < query.length(); ++i)
{
query[i] = 'a';
}
start = myLowerBound(rwords, query);
//start = lower_bound(rwords.begin(), rwords.end(), query, compare);

for (int i = pos; i < query.length(); ++i)
{
query[i] = 'z';
}
end = myUpperBound(rwords, query);
//end = upper_bound(rwords.begin(), rwords.end(), query, compare);
}
else
{
pos = query.find("?");
for (int i = pos; i < query.length(); ++i)
{
query[i] = 'a';
}
start = myLowerBound(words, query);
//start = lower_bound(rwords.begin(), rwords.end(), query, compare);

for (int i = pos; i < query.length(); ++i)
{
query[i] = 'z';
}
end = myUpperBound(words, query);
//end = upper_bound(rwords.begin(), rwords.end(), query, compare);
}
}
}
``````

주석한대로 stl 라이브러리 쓰면 시간초과 안나는데
무슨 문제인가요??

###### 작성중인 코드―solution.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
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
bool compare(string a, string b)
{
if (a.length() == b.length())
return a < b;
else
{
return a.length() < b.length();
}
}
bool lower_compare(string a, string b)
{
if (a.length() == b.length())
return a > b;
else
{
return a.length() > b.length();
}
}
bool upper_compare(string a, string b)
{
if (a.length() == b.length())
return a >= b;
else
{
return a.length() >= b.length();
}
}
int myLowerBound(vector<string> words, string key)
{
int left = 0;
int right = words.size();
int middle = 0;
while (left < right)
{
middle = (left + right) / 2;
if (lower_compare(key, words[middle]))
left = middle +1;
else
right = middle;
}
return right;
}
int myUpperBound(vector<string> words, string key)
{
int left = 0;
int right = words.size();
int middle = 0;
while (left < right)
{
middle = (left + right) / 2;
if (upper_compare(key, words[middle]))
left = middle + 1;
else
right = middle;
}
return right;
}
vector<int> solution(vector<string> words, vector<string> queries)
{
vector<string> rwords;
sort(words.begin(), words.end(), compare);
for (int i = 0; i < words.size(); ++i)
{
string temp = words[i];
reverse(temp.begin(), temp.end());
rwords.push_back(temp);
}
sort(rwords.begin(), rwords.end(), compare);
for (int i = 0; i < queries.size(); ++i)
{
string query = queries[i];
int start, end, pos;
if (query[0] == '?')
{
reverse(query.begin(), query.end());
pos = query.find("?");
for (int i = pos; i < query.length(); ++i)
{
query[i] = 'a';

}
start = myLowerBound(rwords, query);
for (int i = pos; i < query.length(); ++i)
{
query[i] = 'z';
}
end = myUpperBound(rwords, query);
}
else
{
pos = query.find("?");
for (int i = pos; i < query.length(); ++i)
{
query[i] = 'a';
}
start = myLowerBound(words, query);

for (int i = pos; i < query.length(); ++i)
{
query[i] = 'z';
}
end = myUpperBound(words, query);
}