본문 바로가기

Algorithm/Baekjoon

백준(2941번) - 크로아티아 알파벳

백준(2941번) - 크로아티아 알파벳

 

2941번: 크로아티아 알파벳

예전에는 운영체제에서 크로아티아 알파벳을 입력할 수가 없었다. 따라서, 다음과 같이 크로아티아 알파벳을 변경해서 입력했다. 크로아티아 알파벳 변경 č c= ć c- dž dz= đ d- lj lj nj nj š s= ž z=

www.acmicpc.net

 

문제

예전에는 운영체제에서 크로아티아 알파벳을 입력할 수가 없었다. 따라서, 다음과 같이 크로아티아 알파벳을 변경해서 입력했다.

크로아티아 알파벳

 

예를 들어, ljes=njak은 크로아티아 알파벳 6개(lj, e, š, nj, a, k)로 이루어져 있다. 단어가 주어졌을 때, 몇 개의 크로아티아 알파벳으로 이루어져 있는지 출력한다.

dž는 무조건 하나의 알파벳으로 쓰이고, d와 ž가 분리된 것으로 보지 않는다. lj와 nj도 마찬가지이다. 위 목록에 없는 알파벳은 한 글자씩 센다.

입력

첫째 줄에 최대 100글자의 단어가 주어진다. 알파벳 소문자와 '-', '='로만 이루어져 있다.

단어는 크로아티아 알파벳으로 이루어져 있다. 문제 설명의 표에 나와있는 알파벳은 변경된 형태로 입력된다.

출력

입력으로 주어진 단어가 몇 개의 크로아티아 알파벳으로 이루어져 있는지 출력한다.

 

 

입출력 예제

설명

  • 이번 문제도 아스키코드를 사용한다. (핵심은 크로아티아 단어를 한번에 비교하는게 아니라 문자 하나씩 쪼개서 비교해야 된다.)
  • 입력받은 문자열을 차례대로 한글자씩 비교하면서 크로아티아 알파벳과 일반 알파벳을 찾아가면서 개수를 count하고 출력하면 된다. 
  • 이때 문자열의 startIndex와  endIndex를 두고 일반 알파벳은 startIndex++ / 크로아티아 알파벳은 startIndex +=2 or startIndex +=3  을 적용하면서 검색하는 위치를 이동시키면 된다. 
  • 말은 조금 복잡할 수 있으니  코드를 보자!!

주의할점 : charAt() 으로 문자를 가져올 때 index를 초과하면 에러가 나기 때문에 조건을 넣어줘야 된다.

코드

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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Main {
    
    // 2941번 크로아티아 알파벳
    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        int cnt = 0;
        int startIndex = 0;
        int endIndex = str.length()-1;
        while(startIndex <= endIndex) {
            startIndex += nextStartLocation(str,startIndex,endIndex);
            cnt++;
        }
        System.out.println(cnt);
    }
 
    public static int nextStartLocation(String str, int startIndex, int endIndex) {
        if(startIndex == endIndex) return 1;
 
        int compareCharacter = str.charAt(startIndex);
            // =(61), -(45), j(106)
            switch(compareCharacter) {
                case 99// c
                    compareCharacter = str.charAt(startIndex+1);
                    if(compareCharacter == 61 || compareCharacter == 45return 2;
                    return 1;
                case 100// d
                    compareCharacter = str.charAt(startIndex+1);
                    if(compareCharacter == 45return 2;
                    else if( endIndex - startIndex != 1 && compareCharacter == 122 && str.charAt(startIndex+2== 61return 3;
                    return 1;
                case 108// l
                    if(str.charAt(startIndex+1== 106return 2;
                    return 1;
                case 110// n
                    if(str.charAt(startIndex+1== 106return 2;
                    return 1;
                case 115// s
                    if(str.charAt(startIndex+1== 61return 2;
                    return 1;
                case 122// z
                    if(str.charAt(startIndex+1== 61return 2;
                    return 1;
            }
        return 1;
    }
}
cs

'Algorithm > Baekjoon' 카테고리의 다른 글

백준 (1712번) - 손익분기점  (0) 2021.07.31
백준 (1316번) - 그룹 단어 체커  (0) 2021.07.30
백준(5622번) - 다이얼  (0) 2021.07.25
백준(2908번) 상수  (0) 2021.07.25
백준(1152번) 단어의 개수  (0) 2021.07.25