상세 컨텐츠

본문 제목

백준#1181 - 단어 정렬

C#/알고리즘

by McRobbin 2020. 4. 20. 11:35

본문

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

 

1181번: 단어 정렬

첫째 줄에 단어의 개수 N이 주어진다. (1≤N≤20,000) 둘째 줄부터 N개의 줄에 걸쳐 알파벳 소문자로 이루어진 단어가 한 줄에 하나씩 주어진다. 주어지는 문자열의 길이는 50을 넘지 않는다.

www.acmicpc.net

정렬 문제로 분류된 1181 단어 정렬 문제 입니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _1181
{
    class Program
    {
        public class MyComparer : IComparer<string>
        {
            public int Compare(string s1, string s2)
            {
                if (s1.Length.CompareTo(s2.Length) == 0)
                    return s1.CompareTo(s2);
 
                return s1.Length.CompareTo(s2.Length);
            }
        }
 
 
        static void Main(string[] args)
        {
            int count = int.Parse(Console.ReadLine());
 
            var stringList = new List<string>();
            for (int i = 0; i < count; i++)
                stringList.Add(Console.ReadLine());
 
            stringList.Sort(new MyComparer());
 
            for(int i = 0; i < stringList.Count - 1; i++)
            {
                if (stringList[i] == stringList[i + 1])
                {
                    stringList.RemoveAt(i);
                    i--;
                }
            }
 
            foreach (string str in stringList)
                Console.WriteLine(str);
        }
    }
}
 
 
 

 

string을 입력받고 IComparer 인터페이스 상속 후 Compare메서드 구현했습니다.

소팅의 방법은

 

https://programming-mr.tistory.com/46

 

C# List Sort예제.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69..

programming-mr.tistory.com

여기 있습니다.

 

소트후 똑같은 문자를 삭제하고 출력했습니다.

'C# > 알고리즘' 카테고리의 다른 글

백준#10814 - 나이순 정렬  (0) 2020.04.20
백준#11650 - 좌표 정렬하기  (0) 2020.04.20
백준#1026 - 보물  (0) 2020.04.20
백준#1427 - 소트인사이드  (0) 2020.04.19
백준#1543 - 문서 검색  (0) 2020.04.18

관련글 더보기