forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path4.cpp
61 lines (53 loc) Β· 2.31 KB
/
4.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
#include <bits/stdc++.h>
using namespace std;
// κ°μ΄ [left_value, right_value]μΈ λ°μ΄ν°μ κ°μλ₯Ό λ°ννλ ν¨μ
int countByRange(vector<string>& v, string leftValue, string rightValue) {
vector<string>::iterator rightIndex = upper_bound(v.begin(), v.end(), rightValue);
vector<string>::iterator leftIndex = lower_bound(v.begin(), v.end(), leftValue);
return rightIndex - leftIndex;
}
// λ¬Έμμ΄ λ΄μμ νΉμ ν λ¬Έμμ΄μ λ€λ₯Έ λ¬Έμμ΄λ‘ λͺ¨λ μΉννλ ν¨μ
string replaceAll(string str, string from, string to){
string res = str;
int pos = 0;
while((pos = res.find(from, pos)) != string::npos)
{
res.replace(pos, from.size(), to);
pos += to.size();
}
return res;
}
// λͺ¨λ λ¨μ΄λ€μ κΈΈμ΄λ§λ€ λλμ΄μ μ μ₯νκΈ° μν 리μ€νΈ
vector<string> arr[10001];
// λͺ¨λ λ¨μ΄λ€μ κΈΈμ΄λ§λ€ λλμ΄μ λ€μ§μ΄ μ μ₯νκΈ° μν 리μ€νΈ
vector<string> reversed_arr[10001];
vector<int> solution(vector<string> words, vector<string> queries) {
vector<int> answer;
// λͺ¨λ λ¨μ΄λ₯Ό μ λ―Έμ¬ μμΌλμΉ΄λ λ°°μ΄, μ λμ¬ μμΌλμΉ΄λ λ°°μ΄μ κ°κ° μ½μ
for (int i = 0; i < words.size(); i++) {
string word = words[i];
arr[word.size()].push_back(word); // λ¨μ΄λ₯Ό μ½μ
reverse(word.begin(), word.end());
reversed_arr[word.size()].push_back(word); // λ¨μ΄λ₯Ό λ€μ§μ΄μ μ½μ
}
// μ΄μ§ νμμ μννκΈ° μν΄ κ° λ¨μ΄ 리μ€νΈ μ λ ¬ μν
for (int i = 0; i < 10001; i++) {
sort(arr[i].begin(), arr[i].end());
sort(reversed_arr[i].begin(), reversed_arr[i].end());
}
// 쿼리λ₯Ό νλμ© νμΈνλ©° μ²λ¦¬
for (int i = 0; i < queries.size(); i++) {
string q = queries[i];
int res = 0;
if (q[0] != '?') { // μ λ―Έμ¬μ μμΌλ μΉ΄λκ° λΆμ κ²½μ°
res = countByRange(arr[q.size()], replaceAll(q, "?", "a"), replaceAll(q, "?", "z"));
}
else { // μ λμ¬μ μμΌλ μΉ΄λκ° λΆμ κ²½μ°
reverse(q.begin(), q.end());
res = countByRange(reversed_arr[q.size()], replaceAll(q, "?", "a"), replaceAll(q, "?", "z"));
}
// κ²μλ λ¨μ΄μ κ°μλ₯Ό μ μ₯
answer.push_back(res);
}
return answer;
}