본 블로그는 비상업적, 비영리적 용도의 학업만을 위해 글을 게시합니다.
#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;
}
#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;
}