문제는 딱히 복사해오지 않겠습니다...
Dictionary<int, string> 으로, 1번부터 증가한다는 것을 유의한다.
입력받은 것에 대해 타입을 검사하고, 해당하는 답을 출력하도록 한다.
밸류값으로 역으로 키값을 찾는 것은 이 글을 참고하였다. (FirstOrDefault)
namespace SongE
{
public class Program
{
static void Main(string[] args)
{
using var input = new System.IO.StreamReader(Console.OpenStandardInput());
using var print = new System.IO.StreamWriter(Console.OpenStandardOutput());
Dictionary<int, string> pokemon = new();
int[] n = Array.ConvertAll(input.ReadLine().Split(), s => int.Parse(s));
for(int i = 1; i <= n[0]; i++)
{
pokemon.Add(i, input.ReadLine());
}
for(int i = 0; i < n[1]; i++)
{
string q = input.ReadLine();
if (int.TryParse(q, out int qInt))
{
print.WriteLine(pokemon[int.Parse(q)]);
}
else
{
//밸류값으로 키값 찾기
print.WriteLine(pokemon.FirstOrDefault(entry => EqualityComparer<string>.Default.Equals(entry.Value, q)).Key);
}
}
}
}
}
시간초과가 났다.
using System.Text;
namespace SongE
{
public class Program
{
static void Main(string[] args)
{
using var input = new System.IO.StreamReader(Console.OpenStandardInput());
using var print = new System.IO.StreamWriter(Console.OpenStandardOutput());
StringBuilder sb = new StringBuilder();
Dictionary<int, string> pokemon = new();
int[] n = Array.ConvertAll(input.ReadLine().Split(), s => int.Parse(s));
for(int i = 1; i <= n[0]; i++)
{
pokemon.Add(i, input.ReadLine());
}
for(int i = 0; i < n[1]; i++)
{
string q = input.ReadLine();
if (int.TryParse(q, out int qInt))
{
sb.AppendLine(pokemon[qInt]);
}
else
{
//밸류값으로 키값 찾기
sb.AppendLine($"{pokemon.FirstOrDefault(x => x.Value == q).Key}");
}
}
print.WriteLine(sb.ToString());
}
}
}
시간 초 과
여기서 학원선생님께 여쭤봐서 어느 부분이 문제일까 여쭈어봤더니, 밸류값으로 찾는 것은 시간 코스트가 많이 든다고 하셨다. 메모리 문제가 없다면 그냥 딕셔너리를 두개를(서로 키값과 밸류값이 반대인) 만들어보라고 하셨고, 해결되었다.
using System.Text;
namespace SongE
{
public class Program
{
static void Main(string[] args)
{
using var input = new System.IO.StreamReader(Console.OpenStandardInput());
using var print = new System.IO.StreamWriter(Console.OpenStandardOutput());
StringBuilder sb = new StringBuilder();
Dictionary<int, string> pokemon = new();
Dictionary<string, int> pokemon2 = new();
int[] n = Array.ConvertAll(input.ReadLine().Split(), s => int.Parse(s));
for(int i = 1; i <= n[0]; i++)
{
string s = input.ReadLine();
pokemon.Add(i, s);
pokemon2.Add(s, i);
}
for(int i = 0; i < n[1]; i++)
{
string q = input.ReadLine();
if (int.TryParse(q, out int qInt))
{
sb.AppendLine(pokemon[qInt]);
}
else
{
//밸류값으로 키값 찾기
sb.AppendLine($"{pokemon2[q]}");
}
}
print.WriteLine(sb.ToString());
}
}
}
허망하군요... 확실히 그 부분이 많이 걸릴거같아서 좀 고쳤었는데 그냥 밸류값으로 찾는게 코스트가 많이 드는구나