본문 바로가기
알고리즘/프로그래머스

[프로그래머스] 오픈채팅방 c++

by 오오오니 2023. 2. 17.

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;
}