본문 바로가기
Programming/Algorithm

[백준] 2941번 : 크로아티아 알파벳 (Java)

by 안녕주 2021. 7. 25.

문제

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

크로아티아 알파벳변경

č c=
ć c-
dz=
đ d-
lj lj
nj nj
š s=
ž z=

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

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

입력

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

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

출력

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


코드

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int count = 0;
        sc.close();

        for(int i = 0; i < str.length(); i++){
            if (str.charAt(i) == 'c'){
                if (i < str.length() -1){
                    if (str.charAt(i+1) == '='){
                        i += 1;
                    }
                    else if (str.charAt(i+1) == '-'){
                        i += 1;
                    }
                }
            }
            else if (str.charAt(i) == 'd'){
                if(i < str.length() -1){
                    if (str.charAt(i+1) == 'z'){
                        if(i < str.length() -2){
                            if (str.charAt(i+2)== '='){
                                i += 2;
                            }
                            else{
                                i +=1;
                                count +=1;
                            }
                        }
                    }
                    else if (str.charAt(i+1) == '-'){
                        i += 1;
                    }
                }
            }
            else if (str.charAt(i) == 'l'){
                if (i < str.length()-1){
                    if (str.charAt(i+1) == 'j'){
                        i += 1;
                    }
                }
            }
            else if (str.charAt(i) == 'n'){
                if (i < str.length()-1){
                    if (str.charAt(i+1) == 'j'){
                        i += 1;
                    }
                }
            }
            else if (str.charAt(i) == 's'){
                if ( i < str.length()-1){
                    if (str.charAt(i+1) == '='){
                        i += 1;
                    }
                }
            }
            else if (str.charAt(i) == 'z'){
                if ( i < str.length() - 1){
                    if (str.charAt(i+1) == '='){
                        i += 1;
                    }
                }
            }
            count += 1;
        }
        System.out.println(count);
    }
}

풀이

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int count = 0;
        sc.close();
        String[] arr = new String[str.length()];

        for(int i = 0; i < str.length(); i++){
            if (str.charAt(i) == 'c'){
                if (str.charAt(i+1) == '='){
                    i += 1;
                    count += 1;
                }
                else if (str.charAt(i+1) == '-'){
                    i += 1;
                    count += 1;
                }
                else{
                    count +=1;
                }
            }
            else if (str.charAt(i) == 'd'){
                if (str.charAt(i+1) == 'z'){
                    if (str.charAt(i+2)== '='){
                        i += 2;
                        count += 1;
                    }
                    else{
                        i +=1;
                        count +=2;
                    }
                    
                }
                else if (str.charAt(i+1) == '-'){
                    i += 1;
                    count += 1;
                }
                else{
                    count +=1;
                }
            }
            else if (str.charAt(i) == 'l'){
                if (str.charAt(i+1) == 'j'){
                    i += 1;
                    count += 1;
                }
                else{
                    count +=1;
                }
            }
            else if (str.charAt(i) == 'n'){
                if (str.charAt(i+1) == 'j'){
                    i += 1;
                    count += 1;
                }
                else{
                    count +=1;
                }
            }
            else if (str.charAt(i) == 's'){
                if (str.charAt(i+1) == '='){
                    i += 1;
                    count += 1;
                }
                else{
                    count +=1;
                }
            }
            else if (str.charAt(i) == 'z'){
                if (str.charAt(i+1) == '='){
                    i += 1;
                    count += 1;
                }
                else{
                    count +=1;
                }
            }
            else{
                count += 1;
            }
        }

        System.out.println(count);
    }
}

처음의 코드는 이렇게 길었다.... 역시나 런타임 에러가 났다. 

런타임 에러가 정확히 어떤것을 뜻하는지 잘 몰라 구글링을 해 보았다.

  1. 배열에 할당된 크기를 넘어서 접근했을 때
  2. 전역 배열의 크기가 메모리 제한을 초과할 때
  3. 지역 배열의 크기가 스택 크기 제한을 넘어갈 때
  4. 0으로 나눌 떄
  5. 라이브러리에서 예외를 발생시켰을 때
  6. 재귀 호출이 너무 깊어질 때
  7. 이미 해제된 메모리를 또 참조할 때

등등 위와 같은 경우일 떄 런타임 에러가 나는 것이다. 실제로 코드를 돌려보면 에러가 뜨지는 않았지만 저런 배열의 참조범위를 넘어서는 경우 등 세세한 부분까지 잘 생각해봐야겠다는 생각을 하게 되었다. 

 

정답 코드는 굉장히 길었다. 다른 경우는 생각이 나지 않아서 일단 길게 if,else 케이스를 다 나눠서 생각을 했다.

런타임 에러부분은

if (i < str.length() -1){}

if(i < str.length() -2){}

와 같은 코드를 추가함으로써 해결할 수 있었다. 위와 같은 제어코드가 있음으로서 str의 참조 범위를 넘어서는 부분에 대해 접근할 수 없을 것이다.

 

댓글