Files
2019-03-15 13:47:54 +04:00

63 lines
1.4 KiB
Java

package chelper;
import java.util.Arrays;
import io.InputReader;
import io.OutputWriter;
public class D {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
char[] seq = in.nextLine().toCharArray();
int x[] = new int[n];
int y[] = new int[n];
int currentDepth = 0;
int maxDepth = 0;
int w = 0;
for (int i = 0; i < n; i++) {
if (seq[i] == '[') {
currentDepth++;
w++;
}
maxDepth = Math.max(maxDepth, currentDepth);
y[i] = currentDepth;
if (seq[i] == ']') {
currentDepth--;
w++;
if (seq[i - 1] == '[') {
w += 3;
}
}
x[i] = w - 1;
}
int h = maxDepth * 2 + 1;
char[][] answer = new char[h][w];
for (int i = 0; i < h; i++) {
Arrays.fill(answer[i], ' ');
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= maxDepth - y[i]; j++) {
answer[h / 2 - j][x[i]] = '|';
answer[h / 2 + j][x[i]] = '|';
}
answer[h / 2 - (maxDepth - y[i] + 1)][x[i]] = '+';
answer[h / 2 + (maxDepth - y[i] + 1)][x[i]] = '+';
int dx = seq[i] == '[' ? 1 : -1;
answer[h / 2 - (maxDepth - y[i] + 1)][x[i] + dx] = '-';
answer[h / 2 + (maxDepth - y[i] + 1)][x[i] + dx] = '-';
}
for (int i = 0; i < h; i++) {
out.println(new String(answer[i]));
}
}
}