https://school.programmers.co.kr/learn/courses/30/lessons/42888
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
1. 어떤 아이디가 어떤 이름을 가지는지는 map을 이용했다.
아이디1 | 들어왔습니다. |
아이디1 | 나갔습니다. |
아이디1 | 들어왔습니다. |
아이디3 | 들어왔습니다. |
2. 위와같이 어떤아이디가 들어오고 나왔는지 배열에 저장해준다.
3. 아이디의 이름이 바뀌면 map에서 바꾸어준다.
4. 아이디를 map에서 찾아서 2의 배열을 출력해준다.
#include <string>
#include <vector>
#include<map>
using namespace std;
string word1,word2,word3;
map<string, string>m;
string ans[100001][2];
int idx,idx2,word_size;
vector<string> solution(vector<string> record) {
vector<string> answer;
int size=record.size();
for(int i=0;i<size;i++)
{
word1 = record[i][0];
word2="";word3="";
if(word1=="C")
idx=7;
else
idx=6;
word_size=record[i].size();
//단어 자르기
for(int j=idx;j<word_size;j++)
{
if(record[i][j]==' ')
{
idx=j+1;
break;
}
word2+=record[i][j];
}
for(int j=idx;j<word_size;j++)
{
if(record[i][j]==' ')
break;
word3+=record[i][j];
}
if(word1=="E")
{
ans[idx2][0]=word2;
m[word2]=word3;
ans[idx2][1]="님이 들어왔습니다.";
idx2++;
}
else if(word1=="L")
{
ans[idx2][0]=word2;
ans[idx2][1]="님이 나갔습니다.";
idx2++;
}
else//Change
{
m[word2]=word3;
}
}
for(int i=0;i<idx2;i++)
answer.push_back(m[ans[i][0]]+ans[i][1]);
return answer;
}
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 멀쩡한 사각형 c++ (0) | 2023.03.02 |
---|---|
[프로그래머스] 괄호변환 c++ (0) | 2023.02.24 |
[프로그래머스] 전화번호 목록 c++ (0) | 2023.02.17 |
[프로그래머스] 삼각 달팽이 c++ (0) | 2023.02.05 |
[프로그래머스] 완주하지 못한 선수 c++ (0) | 2023.02.05 |