백준 17413 알고리즘
https://www.acmicpc.net/problem/17413
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"bufio"
"os"
)
type Stack struct {
data []rune
size int
}
func New() *Stack {
return &Stack{
data: make([]rune, 0),
size: 0,
}
}
func (stack *Stack) Push(param rune) {
stack.data = append(stack.data, param)
stack.size++
}
func (stack *Stack) Pop() rune {
if len(stack.data) == 0 {
return ' '
}
stack.size--
result := stack.data[stack.size]
stack.data = stack.data[:stack.size]
return result
}
func (stack *Stack) Size() int {
return stack.size
}
func main() {
w := bufio.NewWriter(os.Stdout)
defer w.Flush()
sc := bufio.NewScanner(os.Stdin)
sc.Buffer(make([]byte, 100001), 100001)
sc.Scan()
text := sc.Text()
stack := New()
var tagArea bool = false
for _, word := range text {
if word == '<' { // '<' 일 경우
// 태그 내부 시작
tagArea = true
if stack.Size() > 0 {
reverseWord(stack, w)
}
w.WriteRune(word)
} else if word == '>' { // '>' 일 경우
// 태그 내부 끝
tagArea = false
w.WriteRune(word)
} else if word == ' ' { // 띄어쓰기 일 경우
if stack.Size() > 0 {
reverseWord(stack, w)
}
w.WriteRune(word)
} else {
// 태그 내부 단어일 경우
if tagArea {
w.WriteRune(word)
continue
}
stack.Push(word)
}
}
if stack.Size() > 0 {
reverseWord(stack, w)
}
}
func reverseWord(stack *Stack, result *bufio.Writer) {
for stack.Size() > 0 {
result.WriteRune(stack.Pop())
}
}