[14425] 문자열 집합

RudinP·2023년 4월 10일
0

BaekJoon

목록 보기
28/77

총 N개의 문자열로 이루어진 집합 S가 주어진다.
입력으로 주어지는 M개의 문자열 중에서 집합 S에 포함되어 있는 것이 총 몇 개인지 구하는 프로그램을 작성하시오.

입력

  • 첫째 줄에 문자열의 개수 N과 M (1 ≤ N ≤ 10,000, 1 ≤ M ≤ 10,000)이 주어진다.

다음 N개의 줄에는 집합 S에 포함되어 있는 문자열들이 주어진다.

다음 M개의 줄에는 검사해야 하는 문자열들이 주어진다.

입력으로 주어지는 문자열은 알파벳 소문자로만 이루어져 있으며, 길이는 500을 넘지 않는다. 집합 S에 같은 문자열이 여러 번 주어지는 경우는 없다.

출력

  • 첫째 줄에 M개의 문자열 중에 총 몇 개가 집합 S에 포함되어 있는지 출력한다.

생각

Hashtable을 이용한다.
Contains 메소드를 활용하여 있다면 count를 증가시키고,
count를 출력한다.

처음 코드

using System.Collections;

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());

            Hashtable ht = new();
            int[] nm = Array.ConvertAll(input.ReadLine().Split(), s => int.Parse(s));

            for (int i = 0; i < nm[0]; i++)
            {
                ht.Add(input.ReadLine(), 0);
            }

            int count = 0;
            for (int i = 0; i < nm[1]; i++)
            {
                if (ht.ContainsKey(input.ReadLine()))
                    count++;
            }

            print.Write($"{count}");

        }
    }
}

profile
곰을 좋아합니다. <a href = "https://github.com/RudinP">github</a>

0개의 댓글