https://www.acmicpc.net/problem/1181
1181번: 단어 정렬
첫째 줄에 단어의 개수 N이 주어진다. (1≤N≤20,000) 둘째 줄부터 N개의 줄에 걸쳐 알파벳 소문자로 이루어진 단어가 한 줄에 하나씩 주어진다. 주어지는 문자열의 길이는 50을 넘지 않는다.
www.acmicpc.net
- 길이가 짧은 것부터
- 길이가 같으면 사전 순으로
풀이
IComparer 인터페이스 상속받는 클래스를 만들고
string 받아서 길이가 같다면 단어 오름차순 정렬, 길이가 같지 않다면 길이로 오름차순 정렬 후
반환하는 Compare 메서드 만듬
단어 배열을 만들어서 중복 값은 배열에 안담고 중복안되는 단어만 담음
List Sort로 정렬하는데 상단 Compare 구현한 것을 이용 후 foreach 돌려서 출력
List Sort 이용 시, Compare 구현 필수! 구현안하면 그냥 사전순 오름차순으로 정렬됨
근데 Find사용해서 버전 안맞는다고 컴파일 에러남.. 밑에 사용안하는 코드 따로 해놓음
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
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _1181
{
class Program
{
class Comparer : IComparer<string>
{
public int Compare(string x, string y)
{
if (x.Length == y.Length)
return x.CompareTo(y);
return x.Length.CompareTo(y.Length);
}
}
static void Main(string[] args)
{
var N = int.Parse(Console.ReadLine());
List<string> arr = new List<string>();
if (N >= 1 && N <= 20000)
{
for(int i = 0; i < N; i++)
{
var input = Console.ReadLine();
if(arr.Find(x=>x==input) is null)
{
arr.Add(input);
}
}
}
arr.Sort(new Comparer());
foreach (var word in arr)
{
Console.WriteLine(word);
}
}
}
}
|
cs |
입력받을때 다 단어 배열에 담은 후에 Distinct 사용해서 중복 없애고
다시 List로 바꿔서 변수에 담음
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
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _1181
{
class Program
{
class Comparer : IComparer<string>
{
public int Compare(string x, string y)
{
if (x.Length == y.Length)
return x.CompareTo(y);
return x.Length.CompareTo(y.Length);
}
}
static void Main(string[] args)
{
var N = int.Parse(Console.ReadLine());
List<string> arr = new List<string>();
if (N >= 1 && N <= 20000)
{
for(int i = 0; i < N; i++)
{
var input = Console.ReadLine();
arr.Add(input);
}
}
arr = arr.Distinct().ToList();
arr.Sort(new Comparer());
foreach (var word in arr)
{
Console.WriteLine(word);
}
}
}
}
|
cs |
Compare 구현안하면 밑에처럼도 사용가능
쿼리키워드인 LINQ의 orderby 사용 길이 정렬 후 단어 정렬한 것임
orderby 절의 여러 키는 ThenBy 메서드 호출로 자동 변환됨
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
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _1181
{
class Program
{
static void Main(string[] args)
{
var N = int.Parse(Console.ReadLine());
List<string> arr = new List<string>();
if (N >= 1 && N <= 20000)
{
for(int i = 0; i < N; i++)
{
var input = Console.ReadLine();
arr.Add(input);
}
}
arr = arr.Distinct().ToList();
var arry = from word in arr
orderby word.Length, word
select word;
foreach (var word in arry)
{
Console.WriteLine(word);
}
}
}
}
|
cs |
실행창
'c# > 알고리즘' 카테고리의 다른 글
1302번 베스트셀러 (0) | 2020.06.08 |
---|---|
1343번 폴리오미노 (0) | 2020.06.01 |
2135 문자열 압축하기 - 어려워..나중에 (0) | 2020.05.18 |
9996번 한국이 그리울 땐 서버에 접속하지 (0) | 2020.05.17 |
9933 민균이의 비밀번호 (0) | 2020.05.15 |