Posts [Java] 백준 #2941: 크로아티아 알파벳
Post
Cancel

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

문제

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

크로아티아 알파벳변경
čc=
ćc-
dz=
đd-
ljlj
njnj
šs=
žz=

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

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

풀이

contains()와 replace() 이용

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] croatias = {"c=", "c-", "dz=", "d-", "lj", "nj", "s=", "z="};
        String str = sc.next();
        
        for (int i = 0; i < croatias.length; i++) {
            if (str.contains(croatias[i]))
                str = str.replace(croatias[i], "@");
        }
        System.out.println(str.length());
        sc.close();
    }
}

조건문 이용

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.Scanner;

public class test1 {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String str = sc.next();
    int count = 0;
    int len = str.length();

    for (int i = 0; i < len; i++) {
      char c = str.charAt(i);
      if (c == 'c' && i < len - 1) {
        if (str.charAt(i + 1) == '=' || str.charAt(i + 1) == '=')
          i++; // i + 1까지가 하나의 문자이므로 다음 문자를 건너 뛰기 위해 1 증가
      } else if (c == 'd') {
        if (str.charAt(i + 1) == '-')
          i++;
        else if (str.charAt(i + 1) == 'z' && i < len - 2)
          i += 2;
      }

      else if (c == 'l' || c == 'n' && i < len - 1) {
        if (str.charAt(i + 1) == 'j')
          i++;
      }

      else if (c == 's' || c == 'z' && i < len - 1) {
        if (str.charAt(i + 1) == '=')
          i++;
      }

      count++;
    }
    System.out.println(count);
    sc.close();
  }
}

https://st-lab.tistory.com/68 블로그 참조

특수문자 이용

  • 우선 크로아티아 알파벳을 세는 변수인 count에 문자열의 길이를 저장했다.
  • 크로아티아 문자 조건에 맞을 경우 카운트를 하나 줄인다.
  • 표에 나와 있는 문자 이외에는 기본 알파벳으로 이루어져있을 것이다. 따라서 특수문자(“=”, “-“)를 포함하는 경우에는 무조건 카운트를 줄였다.
  • “dz=”, “lj”, “nj”의 경우를 예외 처리해주었다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Scanner;

public class test1 {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String str = sc.next();
    int count = str.length();

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

https://zorba91.tistory.com/111 블로그 참조

This post is licensed under CC BY 4.0 by the author.

[Java] 백준 #1316: 그룹 단어 체커 (단계별 풀이)

:coffee: [Java] 이것이 자바다 #4: 조건문과 반복문

Loading comments from Disqus ...