[20920] 영단어 암기는 괴로워

RudinP·2023년 4월 24일
0

BaekJoon

목록 보기
59/77

생각

빈도수를 이용하므로 Dictionary를 사용한다.
M값보다 길이가 짧으면 추가하지 않는다.
이후 Dictionary에서 Sort를 한다.
OrderByDescending(x => x.Value).ThenByDescending(x => x.Key.Length).ThenBy(x => x.Key)

처음 코드

namespace SongE
{
    public class Program
    {
        static void Main(string[] args)
        {
            using var input = new System.IO.StreamReader(Console.OpenStandardInput());

            //int intInput() => int.Parse(input.ReadLine());
            int[] intsInput() => Array.ConvertAll(input.ReadLine().Split(), s => int.Parse(s));

            int[] nm = intsInput();
            Dictionary<string, int> dict = new();

            for(int i = 0; i < nm[0]; i++)
            {
                string voca = input.ReadLine();
                if (voca.Length >= nm[1])
                {
                    if(!dict.TryAdd(voca, 0))
                    {
                        dict[voca]++;
                    }
                }
            }

            SortDict(dict);
            
        }

        static void SortDict(Dictionary<string, int> dict)
        {
            using var print = new System.IO.StreamWriter(Console.OpenStandardOutput());

            var sorted = dict.OrderByDescending(x => x.Value).ThenByDescending(x => x.Key.Length).ThenBy(x => x.Key).ToDictionary(x=>x.Key, x=>x.Value);

            foreach(string voca in sorted.Keys)
            {
                print.WriteLine(voca);
            }
        }
        
    }
}

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

0개의 댓글