[SWEA] 스타일리쉬 들여쓰기

문제 보러가기

제한사항

이외의 제한사항은 없다.

첫번째 생각
  1. ’( )’ , ‘{ }’ , ‘[ ]’의 갯수를 줄마다 저장시키는 객체(tab)을 선언한 뒤, 문자열의 정보에 대해 기록한다.
  2. go 함수를 실행하여 줄마다 계산한 뒤, event에 등록한 뒤,
  3. 해당 줄에 해당하는 연산을 수행하며 event를 꺼내서 계산하여 답을 구한다.

JAVA Code

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;

public class Solution {
	static ArrayList<tab> list;
	static ArrayList<tab> event;
	static int Rcnt;
	static int Ccnt;
	static int Scnt;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int tc = 1; tc <= T; tc++) {
			int p = sc.nextInt();
			int q = sc.nextInt();
			list = new ArrayList<>();
			event = new ArrayList<>();
			Rcnt = 0;
			Ccnt = 0;
			Scnt = 0;

			String[] pStr = new String[p];
			for (int i = 0; i < p; i++) {
				pStr[i] = sc.next();
			}
			String[] qStr = new String[q];
			int[] res = new int[q];
			Arrays.fill(res, -2);
			for (int i = 0; i < q; i++) {
				qStr[i] = sc.next();
			}
			int qcount1 =0;
			int qcount2 =0;
			int qcount3 =0;
			for (int i = 0; i < p; i++) {
				char[] ch = pStr[i].toCharArray();
				int cnt = 0;
				while (true) {
					if (ch[cnt] != '.')
						break;
					cnt++;
				}
				if (cnt != 0 ) {
					list.add(new tab(cnt, qcount1, qcount2, qcount3));
				}
				for (int j = 0; j < ch.length; j++) {
					if (ch[j] == '(') {
						qcount1++;
					} else if (ch[j] == ')') {
						qcount1--;
					} else if (ch[j] == '{') {
						qcount2++;
					} else if (ch[j] == '}') {
						qcount2--;
					} else if (ch[j] == '[') {
						qcount3++;
					} else if (ch[j] == ']') {
						qcount3--;
					}
				}
			}
			
			go();
			
			int pcount1 = 0; int pcount2 = 0; int pcount3 = 0;
			System.out.print("#" + tc + " ");
			for (int i = 0; i < q; i++) {
				for(int e=0; e<event.size(); e++) {
					tab tab = event.get(e);
					if ( res[i] == -2 ) {
						res[i] = tab.R * pcount1 + tab.C * pcount2 + tab.S * pcount3;
					} else if (res[i] != tab.R * pcount1 + tab.C * pcount2 + tab.S * pcount3) {
						res[i] = -1;
					}
				}
				if (pcount1 + pcount2 + pcount3 == 0 ) {
					res[i] = 0;
				} else if (event.size() == 0) {
					res[i] = -1;
				}
				
				if (res[i] == -2 ) {
					res[i] = 0;
				}
				System.out.print(res[i]+" ");
				char[] ch = qStr[i].toCharArray();
				for (int j = 0; j < ch.length; j++) {
					if (ch[j] == '(') {
						pcount1++;
					} else if (ch[j] == ')') {
						pcount1--;
					} else if (ch[j] == '{') {
						pcount2++;	
					} else if (ch[j] == '}') {
						pcount2--;
					} else if (ch[j] == '[') {
						pcount3++;
					} else if (ch[j] == ']') {
						pcount3--;
					}
				}
			}
			System.out.println();
		}
	}

	public static void go() {
		for (int R = 1; R <= 20; R++) {
			for (int C = 1; C <= 20; C++) {
				for (int S = 1; S <= 20; S++) {
					for (int i = 0; i < list.size(); i++) {
						tab tab = list.get(i);
						if (tab.cnt == (tab.R * R) + (tab.C * C) + (tab.S * S)) {
							if (i == list.size() - 1) {
								event.add(new tab(0,R,C,S));
							}
						} else {
							break;
						}

					}
				}
			}
		}
	}

	public static class tab {
		int cnt;
		int R;
		int C;
		int S;

		public tab(int Cr, int r, int c, int s) {
			cnt = Cr;
			R = r;
			C = c;
			S = s;
		}

		@Override
		public String toString() {
			return "tab [cnt=" + cnt + ", R=" + R + ", C=" + C + ", S=" + S + "]";
		}
	}
}