7785번: 회사에 있는 사람 (acmicpc.net)
7785번: 회사에 있는 사람
첫째 줄에 로그에 기록된 출입 기록의 수 n이 주어진다. (2 ≤ n ≤ 106) 다음 n개의 줄에는 출입 기록이 순서대로 주어지며, 각 사람의 이름이 주어지고 "enter"나 "leave"가 주어진다. "enter"인 경우는
www.acmicpc.net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace _230122_1
{
class Program
{
static void Main(string[] args)
{
StreamReader sr = new StreamReader(Console.OpenStandardInput());
StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
int N = int.Parse(sr.ReadLine());
Dictionary<string, string> dicLog = new Dictionary<string, string>();
for(int i = 0; i < N; i++)
{
StringBuilder sb = new StringBuilder();
sb.Append(sr.ReadLine());
string[] arr = sb.ToString().Split(' ');
if (dicLog.ContainsKey(arr[0]) && arr[1] == "leave") //입력 시 키값이 중복되면서 leave를 밸류값으로 가진 사람은 딕셔너리에서 지워준다
{
dicLog.Remove(arr[0]);
}
else
{
dicLog.Add(arr[0], arr[1]);
}
}
var dicsort = dicLog.OrderByDescending(x => x.Key); //내림차순 정렬
foreach (var log in dicsort)
{
sw.WriteLine(log.Key);
}
sr.Close();
sw.Flush();
sw.Close();
}
}
}
정답