[알고리즘] 프로그래머스_오픈채팅방

Fortice·2021년 6월 29일
0

알고리즘

목록 보기
13/18

본 블로그는 비상업적, 비영리적 용도의 학업만을 위해 글을 게시합니다.

현재 상황

1. 문제 분석

  • 상당히 귀찮은? 문자열 문제이다.
  • 사실 명령어대로 처리하고 결과를 출력하는거는 쉬워보이는데, STL을 안쓴다면 상당히 복잡할 것 같다.
  • Map을 쓴다면 금방 할거같은데...
  • {유저 id, 닉네임}형태로 매칭시켜놓고, 결과는 결과대로 저장하고, 유저 id와 닉네임 매칭은 따로 관리해주면 될 것 같다.
  • 결과는 명령어-id로 넣어놓다가 나중에 닉네임을 치환해주면 된다.

2. 문제 풀이 과정(삽질)

  • 일단 C로 string도 안쓰고 하려니 미칠 것 같다.
  • 문자열 비교며, 넣고, 빼고 하기도 참으로 어렵다.
  • 복사하기 귀찮아서 포인터로 유저 id와 닉네임을 가리키도록 했는데, 데이터가 남아있어야하니 어쩔 수 없이 동적 할당을 했다.(생각해보니 Enter만 동적할당 쓰면 될 것 같다.)
  • 잠시 접는다.

3. 문제 해결(해결X)

  • 일부의 결과는 잘 나오지만, 전체로 보면 참담하다.
  • C++로 푸니까 바로 해결되긴 한다...

4. 코드(C)

#include <string>
#include <vector>
#include <stdlib.h>
#include <cstring>
#include <iostream>

enum Command {
    Enter = 0,
    Leave = 1,
    Change = 2
};

typedef struct ResultTable{
    int command;
    char** uid_name_address;
}ResultTable;

using namespace std;

int getString(char getter[], string str, int index, char checker)
{
    int getter_index = 0;
    while(str[index] != checker)
        getter[getter_index++] = str[index++];
    for(int i = getter_index; i <= 10; i++)
        getter[getter_index] = 0;
    return getter_index;
}

int getString(char getter[], char str[], int index, char checker)
{
    int getter_index = 0;
    while(str[index] != checker)
        getter[getter_index++] = str[index++];
    for(int i = getter_index; i <= 10; i++)
        getter[getter_index] = 0;
    return getter_index;
}

bool isSame(char * a, char * b)
{
    int i = 0;
    while(a[i] != 0)
    {
        if(a[i] != b[i])
            return false;
        i++;
    }
    return true;
}
void pushResult(ResultTable resultTable[], int result_num, int command, char ** uid_name_address)
{
    resultTable[result_num].command = command;
    resultTable[result_num].uid_name_address = uid_name_address;
}

vector<string> solution(vector<string> record) {
    int people = 0, result = 0;
    int str_index = 0;
    Command command;

    char* uid_name[100000][2];
    string result_tail[2] = {"님이 들어왔습니다.", "님이 나갔습니다."};
    ResultTable resultTable[100000];
    vector<string> answer;
    
    for(int i = 0; i < record.size(); i++)
    {
        str_index = 0;
        
        switch (record[i][0]) 
        {
            case 'E':
                command = Enter;
                str_index += 6;
                break;
            case 'L':
                command = Leave;
                str_index += 6;
                break;
            case 'C':
                command = Change;
                str_index += 7;
                break;
        }
        if(command == Enter)
        {   
            bool isNew= true;
            char * uid = (char *) malloc(sizeof(char) * 11);
            memset(uid, 0, 11);
            str_index += getString(uid, record[i], str_index, ' ') + 1;
            char * name = (char *) malloc(sizeof(char) * 11);
            memset(name, 0, 11);
            str_index += getString(name, record[i], str_index, 0);
            
            for(int j = 0; j < people; j++)
            {
                if(isSame(uid, uid_name[j][0]))
                {
                    getString(uid_name[j][1], name, 0, 0);
                    pushResult(resultTable, result, command, uid_name[j]);
                    isNew = false;
                    break;
                }
            }
            if(isNew)
            {
                uid_name[people][0] = uid;
                uid_name[people][1] = name;
                pushResult(resultTable, result, command, uid_name[people]);
                people++;
            }
            result++;
        }
        else if(command == Leave)
        {
            char uid[11] = {0};
            str_index += getString(uid, record[i], str_index, 0);
            
            for(int j = 0; j < people; j++)
            {
                if(isSame(uid, uid_name[j][0]))
                {
                    pushResult(resultTable, result, command, uid_name[j]);
                    result++;
                }
            }
        }
        else if(command == Change)
        {
            char uid[11] = {0};
            str_index += getString(uid, record[i], str_index, ' ') + 1;

            char name[11] = {0};
            str_index += getString(name, record[i], str_index, 0);
            
            for(int j = 0; j < people; j++)
            {
                if(isSame(uid, uid_name[j][0]))
                {
                    getString(uid_name[j][1], name, 0, 0);
                    break;
                }
            }
        }
    }
    for(int i = 0; i < result; i++)
    {
        string name(resultTable[i].uid_name_address[1]);
        answer.push_back(name + result_tail[resultTable[i].command]);
    }
    return answer;
}

코드(C++)

#include <string>
#include <vector>
#include <utility>
#include <map>

using namespace std;


vector<string> solution(vector<string> record) {
    int start = 0;
    string tail[2] = {"님이 들어왔습니다.", "님이 나갔습니다."};
    vector<pair<string, int>> ResultTable;
    map<string, string> uid_name;
    vector<string> answer;
    
    for(auto i : record)
    {
        start = 0;
        if(i[0] == 'E')
        {
            start += 6;
            int boundary = i.find(' ', start);
            string uid(i, start, boundary++ - start);
            string name(i, boundary, i.length() - boundary);
            
            uid_name[uid] = name;
            ResultTable.push_back({uid, 0});
        }
        else if(i[0] == 'L')
        {
            start += 6;
            string uid(i, start);
            
            ResultTable.push_back({uid, 1});
        }
        else if(i[0] == 'C')
        {
            start += 7;
            int boundary = i.find(' ', start);
            string uid(i, start, boundary++ - start);
            string name(i, boundary, i.length() - boundary);
            
            uid_name[uid] = name;
        }
    }
    for(int i = 0; i < ResultTable.size(); i++)
        answer.push_back(uid_name[ResultTable[i].first] + tail[ResultTable[i].second]);
    return answer;
}
profile
서버 공부합니다.

0개의 댓글