2018λ KAKAO BLIND RECRUITMENT
λ¬Έμ
μ±ν λ°©μ λ€μ΄μ€κ³ λκ°κ±°λ, λλ€μμ λ³κ²½ν κΈ°λ‘μ΄ λ΄κΈ΄ λ¬Έμμ΄ λ°°μ΄ recordκ° λ§€κ°λ³μλ‘ μ£Όμ΄μ§ λ, λͺ¨λ κΈ°λ‘μ΄ μ²λ¦¬λ ν, μ΅μ’ μ μΌλ‘ λ°©μ κ°μ€ν μ¬λμ΄ λ³΄κ² λλ λ©μμ§λ₯Ό λ¬Έμμ΄ λ°°μ΄ ννλ‘ return νλλ‘ solution ν¨μλ₯Ό μμ±νλΌ.
νμ΄
λ¬Έμμ΄μ split ν΄μΌνλ λΆλΆμμ C++μλ ν΄λΉ λΌμ΄λΈλ¬λ¦¬κ° μμ΄μ λ¬Έμμ΄ λ€λ£¨λ λΆλΆμμ κ΅μ₯ν μ μΌλ€.
strtok λ₯Ό νμ©νμ¬ splitκ³Ό κ°μ ν¨κ³Όλ₯Ό λΌ μ μλλ° μλ£νμ΄ char* μ΄κΈ° λλ¬Έμ record μ μλ£νμΈ string μ λ¨Όμ char νμΌλ‘ λ°κΎΌ λ€μ ν¬μΈν°λ₯Ό μ΄μ©νμ¬ λ¬Έμμ΄λ€μ ꡬλΆνλ€.
21, 27, 31 λ² μ€ κ°μ λΆλΆμ
tok = strtok(NULL, " ");
whileλ¬Έ λ΄λΆμ 첫λ²μ§Έ νλΌλ©ν°λ‘ NULLμ μ£Όκ²λλ©΄ λ΄λΆ λ²νΌμ μ μ₯λμ΄μλ ν¬μΈν°μμ λ€μ " "λ₯Ό μ°Ύκ³ κ·Έ λ€μ λ¬Έμμ΄μ μ£Όμλ₯Ό λ°ννκ² λλ€. μ΄λ κ² ν΄μ κ³μν΄μ μννλ©΄ λͺ¨λ λ¬Έμμ΄μ μλΌλΌ μ μλ€.
λ¬Έμμ΄ μ²λ¦¬λ₯Ό λ§μΉκ³ λλ©΄ Enter λλ Change μΌ λλ μΈ λ©μ΄, Leave μΌ λλ λ λ©μ΄λ‘ λλκ² λλ€. μ΄ λ λ§μ§λ§μ ν λ²μ λ¬Έμμ΄λ‘ μΆλ ₯νκΈ° μνμ¬ λͺ¨λ λ΄μ©μ μ 리νμ¬ vector v μ μ μ₯ν΄λλλ° pair μμ μ΄μ©νμ¬ μ²« λ²μ§Έ λͺ λ Ήμ΄ λΆλΆμ΄ Enter μ΄λΌλ©΄ second κ°μ 1μ, Leave λΌλ©΄ second κ°μ 0μ λ£λλ€.
κ·Έλ¦¬κ³ λμ vμ size λ§νΌ λ°λ³΅νμ¬ μ°¨λ‘λ‘ second κ°μ νμΈνμ¬ 1μ΄λΌλ©΄ "λμ΄ λ€μ΄μμ΅λλ€.", 0μ΄λΌλ©΄ "λμ΄ λκ°μ΅λλ€."λ₯Ό λ§μ§λ§ 리ν΄λ λ°°μ΄μΈ answerμ μ μ₯ν΄μ€λ€.
μ½λ
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
|
#include <string>
#include <vector>
#include <map>
using namespace std;
vector<string> solution(vector<string> record) {
typedef pair<string, int> p;
vector<p> v;
vector<string> answer;
map<string, string> user;
string uid, nickname;
for (int i = 0; i < record.size(); i++) {
vector<char> str(record[i].begin(), record[i].end());
str.push_back('\0');
char* p_str = &str[0];
char* tok = strtok(p_str, " ");
if (!strcmp(tok, "Enter")) {
tok = strtok(NULL, " ");
uid = tok;
tok = strtok(NULL, " ");
nickname = tok;
user[uid] = nickname;
v.push_back({ uid, 1 });
}
else if (!strcmp(tok, "Leave")) {
tok = strtok(NULL, " ");
uid = tok;
v.push_back({ uid, 0 });
}
else { // Change
tok = strtok(NULL, " ");
uid = tok;
tok = strtok(NULL, " ");
nickname = tok;
user[uid] = nickname;
}
}
for (int i = 0; i < v.size(); i++) {
if (v[i].second) {
nickname = user[v[i].first];
answer.push_back(nickname + "λμ΄ λ€μ΄μμ΅λλ€.");
}
else {
nickname = user[v[i].first];
answer.push_back(nickname + "λμ΄ λκ°μ΅λλ€.");
}
}
return answer;
}
int main() {
solution({ "Enter uid1234 Muzi", "Enter uid4567 Prodo","Leave uid1234","Enter uid1234 Prodo","Change uid4567 Ryan" });
return 0;
}
|
cs |
λ€λ₯Έ μ¬λ νμ΄ μ°Έκ³ : sstream
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
|
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <map>
using namespace std;
vector<string> solution(vector<string> record) {
vector<string> answer;
string command;
string ID;
string uid;
map<string,string> m;
for(string input:record)
{
stringstream ss(input);
ss>>command;
ss>>uid;
if(command=="Enter" || command=="Change")
{
ss>>ID;
m[uid]=ID;
}
}
for(string input:record)
{
stringstream ss(input);
ss>>command;
ss>>uid;
if(command=="Enter")
{
ID=(m.find(uid)->second);
string temp = ID+"λμ΄ λ€μ΄μμ΅λλ€.";
answer.push_back(temp);
}
else if(command=="Leave")
{
ID=(m.find(uid)->second);
string temp = ID+"λμ΄ λκ°μ΅λλ€.";
answer.push_back(temp);
}
}
return answer;
}
|
cs |
'π₯ PS(Problem Solving) π₯ > programmers' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[programmers] μμ νμ, μΉ΄ν« (0) | 2020.03.11 |
---|---|
[programmers] μμ νμ, λͺ¨μκ³ μ¬ (0) | 2020.03.11 |
[programmers] μμ νμ, μ«μ μΌκ΅¬ (0) | 2020.03.11 |
[programmers] μμ νμ, μμμ°ΎκΈ° (0) | 2020.01.17 |
[νλ‘κ·Έλλ¨Έμ€] #42889 μ€ν¨μ¨ (0) | 2019.09.09 |
λκΈ