Skip to content

Commit 30a83be

Browse files
committed
[Silver III] Title: 단어 뒤집기 2, Time: 1416 ms, Memory: 306152 KB -BaekjoonHub
1 parent 91838b9 commit 30a83be

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# [Silver III] 단어 뒤집기 2 - 17413
2+
3+
[문제 링크](https://www.acmicpc.net/problem/17413)
4+
5+
### 성능 요약
6+
7+
메모리: 306152 KB, 시간: 1416 ms
8+
9+
### 분류
10+
11+
자료 구조, 구현, 스택, 문자열
12+
13+
### 제출 일자
14+
15+
2025년 1월 23일 15:27:08
16+
17+
### 문제 설명
18+
19+
<p>문자열 S가 주어졌을 때, 이 문자열에서 단어만 뒤집으려고 한다.</p>
20+
21+
<p>먼저, 문자열 S는 아래와과 같은 규칙을 지킨다.</p>
22+
23+
<ol>
24+
<li>알파벳 소문자('<code>a</code>'-'<code>z</code>'), 숫자('<code>0</code>'-'<code>9</code>'), 공백('<code> </code>'), 특수 문자('<code><</code>', '<code>></code>')로만 이루어져 있다.</li>
25+
<li>문자열의 시작과 끝은 공백이 아니다.</li>
26+
<li>'<code><</code>'와 '<code>></code>'가 문자열에 있는 경우 번갈아가면서 등장하며, '<code><</code>'이 먼저 등장한다. 또, 두 문자의 개수는 같다.</li>
27+
</ol>
28+
29+
<p>태그는 '<code><</code>'로 시작해서 '<code>></code>'로 끝나는 길이가 3 이상인 부분 문자열이고, '<code><</code>'와 '<code>></code>' 사이에는 알파벳 소문자와 공백만 있다. 단어는 알파벳 소문자와 숫자로 이루어진 부분 문자열이고, 연속하는 두 단어는 공백 하나로 구분한다. 태그는 단어가 아니며, 태그와 단어 사이에는 공백이 없다.</p>
30+
31+
### 입력
32+
33+
<p>첫째 줄에 문자열 S가 주어진다. S의 길이는 100,000 이하이다.</p>
34+
35+
### 출력
36+
37+
<p>첫째 줄에 문자열 S의 단어를 뒤집어서 출력한다.</p>
38+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.Scanner;
2+
import java.util.Stack;
3+
4+
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
5+
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
6+
public class Main {
7+
public static void main(String[] args) {
8+
//태그 구분
9+
//<를 마주쳤다면 >만날떄까지 continue
10+
11+
/*
12+
* 1. if < 마주쳤다면 > 만날때까지 그대로 출력 문자열에 추가
13+
* 2. else 문자를 마주쳤다면, < 마주칠 때까지 문자열을 담아서 < 마주치면 문자열을 공백 기준으로 split
14+
* 3. 각 단어당 뒤에서부터 루프를 돌려서 출력 문자열에 추가
15+
* */
16+
Scanner scanner = new Scanner(System.in);
17+
String s = scanner.nextLine();
18+
String result = "";
19+
Stack<Character> stack = new Stack<>();
20+
21+
int i = 0;
22+
23+
while(i < s.length()){
24+
char c = s.charAt(i);
25+
26+
if(c == '<'){
27+
while(i<s.length() && s.charAt(i) != '>'){
28+
result += s.charAt(i);
29+
i++;
30+
}
31+
result += '>';
32+
i++;
33+
}else if(c != ' '){
34+
while(i<s.length() && s.charAt(i) != ' ' && s.charAt(i) != '<'){
35+
36+
stack.push(s.charAt(i));
37+
i++;
38+
}
39+
while(!stack.isEmpty()){
40+
result += stack.pop();
41+
}
42+
}else{
43+
result += ' ';
44+
i++;
45+
}
46+
}
47+
System.out.println(result);
48+
}
49+
}

0 commit comments

Comments
 (0)