Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Dialog
- 안드로이드
- textfield
- 앱
- textview
- 앱바
- android
- Coroutines
- scroll
- TEST
- livedata
- activity
- intent
- Button
- binding
- 계측
- LifeCycle
- appbar
- tabbar
- DART
- drift
- CustomScrollView
- Compose
- Kotlin
- 테스트
- data
- Navigation
- Flutter
- ScrollView
- viewmodel
Archives
- Today
- Total
Study Record
[프로그래머스] Level2 - 오픈채팅방 본문
728x90
#include <string>
#include <vector>
#include <map>
using namespace std;
string getUid(string str) {
int start = 0, count = 0, end = 0;
while (str[start] != ' ') start++;
end = ++start;
while (end < str.length() && str[end] != ' ') {
end++;
count++;
}
return str.substr(start, count);
}
string getNickname(string str) {
int start = str.length() - 1;
while (str[start] != ' ') start--;
return str.substr(start + 1, str.length() - 1 - start);
}
vector<string> solution(vector<string> record) {
vector<string> answer;
map<string, string> uidToNick;
vector<pair<string, string>> answerTmp; // ([enter | leave], uid)
for(int i=0; i<record.size(); i++){
if(record[i][0] == 'E'){
// Enter 인 경우 uid, nickname 등록
string uid = getUid(record[i]);
if (uidToNick.find(uid) != uidToNick.end()) {
// 이미 존재
uidToNick[uid] = getNickname(record[i]);
} else {
// 존재하지 않음
uidToNick.insert({uid, getNickname(record[i])});
}
answerTmp.push_back(make_pair("님이 들어왔습니다.", uid));
} else if(record[i][0] == 'C'){
// nickname 을 바꾸는 경우
string uid = getUid(record[i]);
uidToNick[uid] = getNickname(record[i]);
} else {
// Leave 의 경우
string uid = getUid(record[i]);
answerTmp.push_back(make_pair("님이 나갔습니다.", uid));
}
}
// answer 만들기
for(int i=0; i<answerTmp.size(); i++){
answer.push_back(uidToNick[answerTmp[i].second] + answerTmp[i].first);
}
return answer;
}
728x90
'알고리즘' 카테고리의 다른 글
[프로그래머스] Level2 - 카카오프렌즈 컬러링북 (0) | 2021.12.02 |
---|---|
[프로그래머스] Level1 - 크레인 인형뽑기 게임 <스택> (0) | 2021.12.02 |
[프로그래머스] Level1 - 키패드 누르기 (0) | 2021.12.01 |
[프로그래머스] Level1 - 숫자 문자열과 영단어 (0) | 2021.11.30 |
[프로그래머스] Level2 - 문자열 압축 (0) | 2021.11.30 |