-
Notifications
You must be signed in to change notification settings - Fork 1
/
Baekjoon5076.java
76 lines (66 loc) · 2.13 KB
/
Baekjoon5076.java
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
package Algorithms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
/**
* https://www.acmicpc.net/problem/5076
* 백준 5076 웹페이지
*/
public class Baekjoon5076 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
while (!"#".equals(input)) {
solution(input);
input = br.readLine();
}
}
private static void solution(String input) {
Stack<String> stack = new Stack<>();
int index = 0;
while(index<input.length()){
char c = input.charAt(index);
if(c == '<'){
StringBuilder sb = new StringBuilder();
int temp = 1;
boolean isClosingTag = false;
if(input.charAt(index+temp) == '/'){
isClosingTag = true;
temp++;
}
while(true){
if(input.charAt(index+temp) != '>'){
sb.append(input.charAt(index+temp));
temp++;
} else {
break;
}
}
index+=temp;
//자기 혼자 열고 닫는 태그
if(sb.toString().charAt(sb.length()-1) == '/'){
continue;
}
//닫는 태그
if(isClosingTag){
if(stack.isEmpty()){
System.out.println("illegal");
return;
} else if(!stack.pop().equals(sb.toString())){
System.out.println("illegal");
return;
}
} else {
stack.push(sb.toString().split(" ")[0]);
}
}
index++;
}
if(stack.isEmpty()){
System.out.println("legal");
} else {
System.out.println("illegal");
}
}
}