[BOJ] 1764 듣보잡

Algorithm 2023. 1. 20. 00:25
반응형

 

https://www.acmicpc.net/problem/1764

 

1764번: 듣보잡

첫째 줄에 듣도 못한 사람의 수 N, 보도 못한 사람의 수 M이 주어진다. 이어서 둘째 줄부터 N개의 줄에 걸쳐 듣도 못한 사람의 이름과, N+2째 줄부터 보도 못한 사람의 이름이 순서대로 주어진다.

www.acmicpc.net

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace _1764
{
    class Program
    {
        static void Main()
        {
            var nm = Array.ConvertAll(Console.ReadLine().Split(' ').ToArray(), int.Parse);
            int n = nm[0];  //듣도 못한 사람 수 
            int m = nm[1];  //보도 못한 사람 수
            Dictionary<string, int> dicN = new Dictionary<string, int>();
            Dictionary<string, int> dicM = new Dictionary<string, int>();
            for (int i = 0; i < n; i++)
            {
                var name = Console.ReadLine();
                dicN[name] = 1;
            }
            for (int i = 0; i < m; i++)
            {
                var name = Console.ReadLine();
                dicM[name] = 1;
            }

            List<string> list = new List<string>();
            foreach (var pair in dicM)
            {
                if (dicN.ContainsKey(pair.Key))
                {
                    list.Add(pair.Key);
                }
            }
            
            list.Sort();    
            Console.WriteLine(list.Count);
            foreach (var name in list)
            {
                Console.WriteLine(name);
            }
            
            
        }
    }
    
}

 

 

 

 

100ms

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace 콘솔테스트
{
    class Program
    { 
        static void Main(string[] args)
        {
            BufferedStream bs = new BufferedStream(Console.OpenStandardInput());
            StreamReader sr = new StreamReader(bs);
            StringBuilder sb = new StringBuilder();
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());

            int[] t = Array.ConvertAll(sr.ReadLine().Split(new char[] {' '}), s => int.Parse(s));
            HashSet<string> Data = new HashSet<string>(t[0]);
            List<string> List = new List<string>();

            for (int i = 0; i < t[0]; i++)
            {
                Data.Add(sr.ReadLine());
            }

            for (int i = 0;i < t[1]; i++)
            {
                string str = sr.ReadLine(); 
                if (Data.Contains(str))
                {
                    List.Add(str);
                }
            }
            
            List.Sort();

            foreach (var item in List)
            {
                sb.AppendLine(item);
            }

            sw.WriteLine(List.Count);
            sw.Write(sb.ToString());

            bs.Close();
            sw.Close();
            sr.Close();
        }
    }
}
반응형

'Algorithm' 카테고리의 다른 글

[BOJ] 1254 팰린드롬 만들기  (0) 2023.01.20
[BOJ] 1159 농구 경기  (0) 2023.01.20
[BOJ] 1251 단어 나누기  (0) 2023.01.20
[프로그래머스] 옹알이 (1)  (0) 2023.01.19
[LeetCode] Two Sum  (0) 2023.01.19
: