C#/알고리즘
백준#1181 - 단어 정렬
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;
namespace _1181
{
class Program
{
public class MyComparer : IComparer<string>
{
public int Compare(string s1, string s2)
{
return s1.CompareTo(s2);
}
}
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());
{
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
여기 있습니다.
소트후 똑같은 문자를 삭제하고 출력했습니다.