perry05

[백준] 2941 - 크로아티아 알파벳(JAVA) 본문

문제풀이 기록/JAVA

[백준] 2941 - 크로아티아 알파벳(JAVA)

perry05 2022. 11. 28. 17:48

[백준] 단계별로 풀어보기 - 문자열

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

 

2941번: 크로아티아 알파벳

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

www.acmicpc.net

 

> 문제

 

> 풀이

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] alphabet = {"c=", "c-", "dz=", "d-", "lj", "nj", "s=", "z="};
		String in;
		int cnt = 0;
		int remainWords;
		
		in = br.readLine();
		remainWords = in.length();
		br.close();
		
		for(int i = 0; i < alphabet.length; i++) {
			int index = in.indexOf(alphabet[i]);
			int l = alphabet[i].length();
			while(index != -1) {
				if(index!=0 && i ==7 && in.charAt(index-1)=='d') { //i==7 ->  "z="
					index = in.indexOf(alphabet[i],index+l);
				}
				else {
					index = in.indexOf(alphabet[i],index+l);
					cnt++;
					remainWords = remainWords - l;
				}
			}
		}
		cnt = cnt + remainWords;
		
		System.out.println(cnt);
	}
}

dz=과 z=을 처리하는 부분이 조금 더러운 것 같아서 제출 후 다른사람 풀이를 봤는데 replace를 활용하면 더 쉽고 간단하다는 것을 알았다.

Comments