문제
예전에는 운영체제에서 크로아티아 알파벳을 입력할 수가 없었다. 따라서, 다음과 같이 크로아티아 알파벳을 변경해서 입력했다.
크로아티아 알파벳변경
č | c= |
ć | c- |
dž | 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);
}
}
처음의 코드는 이렇게 길었다.... 역시나 런타임 에러가 났다.
런타임 에러가 정확히 어떤것을 뜻하는지 잘 몰라 구글링을 해 보았다.
- 배열에 할당된 크기를 넘어서 접근했을 때
- 전역 배열의 크기가 메모리 제한을 초과할 때
- 지역 배열의 크기가 스택 크기 제한을 넘어갈 때
- 0으로 나눌 떄
- 라이브러리에서 예외를 발생시켰을 때
- 재귀 호출이 너무 깊어질 때
- 이미 해제된 메모리를 또 참조할 때
등등 위와 같은 경우일 떄 런타임 에러가 나는 것이다. 실제로 코드를 돌려보면 에러가 뜨지는 않았지만 저런 배열의 참조범위를 넘어서는 경우 등 세세한 부분까지 잘 생각해봐야겠다는 생각을 하게 되었다.
정답 코드는 굉장히 길었다. 다른 경우는 생각이 나지 않아서 일단 길게 if,else 케이스를 다 나눠서 생각을 했다.
런타임 에러부분은
if (i < str.length() -1){}
if(i < str.length() -2){}
와 같은 코드를 추가함으로써 해결할 수 있었다. 위와 같은 제어코드가 있음으로서 str의 참조 범위를 넘어서는 부분에 대해 접근할 수 없을 것이다.
'Programming > Algorithm' 카테고리의 다른 글
[백준] 1712번 : 손익분기점 (Java) (0) | 2021.07.27 |
---|---|
[백준] 1316번 : 그룹 단어 체커 (Java) (0) | 2021.07.25 |
[백준] 5622번 : 다이얼 (Java) (0) | 2021.07.22 |
[백준] 2908번 : 상수 (Java) (0) | 2021.07.22 |
[백준] 1152번 : 단어의 개수 (Java) (0) | 2021.07.21 |
댓글