본문 바로가기
Algorithm/Baekjoon

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

by unknownomad 2022. 3. 22.

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

 

2941번: 크로아티아 알파벳

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

www.acmicpc.net



주의점

  • 크로아티아 알파벳 개수 세기
  • 배열 사용할 때 인덱스가 범위를 벗어나는지 유의해야
  • 8개의 문자는 특정 조건에 의해 변경되어 하나의 문자를 이룸

풀이

1. Scanner

import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
    
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        int cnt = 0;
 
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
 
            if(ch == 'c') {
                /*
                java.lang.StringIndexOutOfBoundsException 예외 방지
                'c' 가 마지막 문자일 때 아래 조건이 없으면
                바로 str.charAt() 참조하여 StringIndexOutOfBoundsException 에러 발생
                */
                if(i < str.length() - 1) {
                    if(str.charAt(i + 1) == '=') { //ch 다음 문자 비교
                        // i + 1 까지가 하나의 문자
                        //다음 문자를 건너 뛰기 위해 +1
                        i++;		
                    } else if(str.charAt(i + 1) == '-') {
                        i++;
                    }
                }
            } else if(ch == '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 if(str.charAt(i + 1) == '-') {
                        i++;
                    }
                }
            } else if(ch == 'l') {
                if(i < str.length() - 1) {
                    if(str.charAt(i + 1) == 'j') {
                        i++;
                    }
                }
            } else if(ch == 'n') {
                if(i < str.length() - 1) {
                    if(str.charAt(i + 1) == 'j') {
                        i++;
                    }
                }
            } else if(ch == 's') {
                if(i < str.length() - 1) {
                    if(str.charAt(i + 1) == '=') {
                        i++;
                    }
                }
            } else if(ch == 'z') {
                if(i < str.length() - 1) {
                    if(str.charAt(i + 1) == '=') {
                        i++;
                    }
                }
            }
            cnt++;
        }
        System.out.println(cnt);
    }
}

 

2. BufferedReader

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Main {
    public static void main(String[] args) throws IOException {
    
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        int len = str.length();
        int cnt = 0;
 
        for (int i = 0; i < len; i++) {
            char ch = str.charAt(i);
 
            if(ch == 'c' && i < len - 1) {
                if(str.charAt(i + 1) == '=' || str.charAt(i + 1) == '-') {		
                    i++;		
                }
            } else if(ch == 'd' && i < len - 1) {
                if(str.charAt(i + 1) == '-') {
                    i++;
                } else if(str.charAt(i + 1) == 'z' && i < len - 2) {
                    if(str.charAt(i + 2) == '=') {
                        i += 2;
                    }
                }
            } else if((ch == 'l' || ch == 'n') && i < len - 1) {
                if(str.charAt(i + 1) == 'j') {
                    i++;
                }
            } else if((ch == 's' || ch == 'z') && i < len - 1) {
                if(str.charAt(i + 1) == '=') {
                    i++;
                }
            }
            cnt++;
        }
        System.out.println(cnt);
    }
}

성능

  • BufferedReader > Scanner

 


출처 : https://st-lab.tistory.com/68

댓글