Recent Posts
Recent Comments
Link
- Today
- Yesterday
- Total
메이쁘
(JAVA) 백준 15904번 : UCPC는 무엇의 약자일까? 본문
https://www.acmicpc.net/problem/15904
15904번: UCPC는 무엇의 약자일까?
첫 번째 줄에 알파벳 대소문자, 공백으로 구성된 문자열이 주어진다. 문자열의 길이는 최대 1,000자이다. 문자열의 맨 앞과 맨 끝에 공백이 있는 경우는 없고, 공백이 연속해서 2번 이상 주어지는
www.acmicpc.net
안녕하세요.
그리디 + 문자열 문제이다.
난이도는 쉬운 편이다.
거두절미하고, 핵심은
"문자열 안에 순서대로 UCPC 가 들어가있으면 I love UCPC, 들어가있지 않으면 I hate UCPC 이다."
UCPC가 붙어있지 않아도 되지만, 반드시 앞에서부터 U - C - P - C 순서대로 들어있어야 한다.
UCPC를 배열에 넣거나, 다른 자료구조를 사용해도 된다.
*** 필자는 큐를 사용해서 front에서 하나씩 꺼냈다.
나머지는 쉽게 풀 수 있을 것이다.
궁금한 점은 하단 소스코드를 참고하면 된다.
이상입니다.
감사합니다!
소스코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.LinkedList; | |
import java.util.Queue; | |
import java.util.StringTokenizer; | |
// 그리디 + 문자열 | |
// UCPC는 무엇의 약자일까? | |
public class p15904 { | |
static Queue<Character> queue; | |
static char compareChar; | |
public static void main(String args[]) throws IOException { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
StringTokenizer st = new StringTokenizer(br.readLine()); | |
// UCPC 넣기 | |
queue = new LinkedList<Character>(); | |
queue.offer('U'); | |
queue.offer('C'); | |
queue.offer('P'); | |
queue.offer('C'); | |
compareChar = queue.poll(); | |
boolean check = false; | |
while(st.hasMoreTokens()) { | |
String str = st.nextToken(); | |
for(int i = 0; i < str.length(); i++) { | |
char alphabet = str.charAt(i); | |
if(compareChar == alphabet) { | |
if(queue.isEmpty()) { | |
check = true; | |
break; | |
} | |
compareChar = queue.poll(); | |
} | |
} | |
if(check) { | |
break; | |
} | |
} | |
if(check) { | |
System.out.print("I love UCPC"); | |
} | |
else { | |
System.out.print("I hate UCPC"); | |
} | |
} | |
} |
'Algorithm > Baekjoon' 카테고리의 다른 글
(JAVA) 백준 1701번 : Cubeditor --- [KMP] (0) | 2020.09.02 |
---|---|
(JAVA) 백준 13305번 : 주유소 (0) | 2020.09.01 |
(JAVA) 백준 2847번 : 게임을 만든 동준이 (0) | 2020.08.30 |
(JAVA) 백준 1699번 : 제곱수의 합 (1) | 2020.08.30 |
(JAVA) 백준 1212번 : 8진수 2진수 (0) | 2020.08.29 |