본문 바로가기
Preparing Coding Test/Baekjoon

[Java(자바)/백준/문자열] 2941번: 크로아티아 알파벳

by weero 2020. 12. 21.

문제

www.acmicpc.net/problem/2941

 

2941번: 크로아티아 알파벳

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

www.acmicpc.net

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

 

č 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.io.*;
import java.util.*;

public class Main{
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str = br.readLine();
		int cnt=0;
		
		for(int i=0; i<str.length(); i++) {
			if(i<str.length()-2 && str.substring(i,i+3).contentEquals("dz=")) {
				//System.out.println(str.substring(i,i+3));
				i+=2;
				++cnt;
				continue;
			}
			
			if(i<str.length()-1) {
				String temp = str.substring(i,i+2);
				if(temp.contentEquals("c=") || temp.contentEquals("c-") || temp.contentEquals("d-") || temp.contentEquals("lj") || temp.contentEquals("nj") || temp.contentEquals("s=") || temp.contentEquals("z=")) {
					//System.out.println(temp);
					i+=1;
					++cnt;
					continue;
				}						
			}
			
			//System.out.println(str.substring(i,i+1));
			++cnt;
			
		}
		System.out.println(cnt);
		
	}
}

 

이런 유형들은 은근 코딩테스트의 문자열 문제에서 많이 접한다.