
1. 문제 풀이 아이디어
- 걸그룹 이름과 멤버 목록을
Dictionary<string, List<string>>
로 저장한다.
- 각 쿼리에서 조건에 따라 팀 이름이나 선수를 출력하여 문제를 해결한다.
2. 나의 정답 코드
StreamReader sr = new(Console.OpenStandardInput());
StreamWriter sw = new(Console.OpenStandardOutput());
string[] split = sr.ReadLine().Split();
int n = int.Parse(split[0]);
int m = int.Parse(split[1]);
Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();
for (int i = 0; i < n; i++)
{
string team = sr.ReadLine();
int tn = int.Parse(sr.ReadLine());
List<string> list = new List<string>();
for (int j = 0; j < tn; j++)
{
list.Add(sr.ReadLine());
}
list.Sort();
dic.Add(team, list);
}
for (int i = 0; i < m; i++)
{
string q = sr.ReadLine();
int qi = int.Parse(sr.ReadLine());
if (qi == 0)
{
List<string> list = dic[q];
foreach (string s in list)
{
sw.WriteLine(s);
}
}
else
{
foreach (var key in dic)
{
if (key.Value.Contains(q))
{
sw.WriteLine(key.Key);
break;
}
}
}
}
sr.Close();
sw.Close();
3. 정리
- 첫 번째 루프에서는
n
개의 팀과 선수 정보를 입력받아Dictionary
에 저장한다.
- 두 번째 루프에서는 쿼리 조건에 맞게 결과를 출력한다.
qi == 0
이면 팀에 속한 선수들을 출력하고,qi == 1
이면 선수의 소속 팀을 출력한다.
Share article