-
백준 2902 - KMP는 왜 KMP일까? (JAVA)Algorithm 2022. 9. 20. 10:23728x90
이번 주 코테 study의 마지막 문제이다.
2902번: KMP는 왜 KMP일까? (acmicpc.net)
KMP와 같은 약자를 만드는 문제인데 나는 단어를 쪼개는 StringTokenizer를 잘 쓰면 해결 될 수 있는 문제라고 생각했다.
※ 알고리즘
1. StringTokenizer를 사용하여 "-" 단위로 단어를 쪼개준다.
2. 단어 마다의 맨 앞 글자를 StringBuilder에 append 시키고 더 이상 단어가 없으면 StringBuilder를 출력한다.
★ hasMoreTokens()
- 주로 while문과 함께 사용되면 StringTokenizer로 구분된 토큰들을 토큰 단위로 찾아주는 메서드
위 처럼 알고리즘에서 말한 내용들을 그대로 옮겨보니 생각보다 쉽게 끝이 난 것을 알 수 있었다. 다 끝나고 보니 그냥 순수하게 대문자만 출력시키는 코딩을 해도 될 것 같기는 한데 그래도 이 문제의 최종 의도는 단어를 구분 지어 그 대문자로 약자를 만드는 것이니 StringTokenizer를 연습시키는 문제가 아닐까 싶다.
728x90'Algorithm' 카테고리의 다른 글
백준 11050 - 이항 계수1(JAVA) (1) 2022.09.25 백준 14467 - 소가 길을 건너간 이유(JAVA) (0) 2022.09.25 백준 1225 - 이상한 곱셈(JAVA) (0) 2022.09.20 백준 2798 - 블랙잭(JAVA) (0) 2022.09.18 백준 2231 - 분해합(JAVA) (0) 2022.09.16