题解:先用括号匹配的思想记录每个括号出现时的大小,然后暴力模拟。需要注意的是输出的结果,其中空字符是空格,由于空字符看不能直接看到,误以为和答案一样结果交上去WA。把符号的阿斯克码输出就方便调试了。
代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define rld(x) scanf("%lld",&(x))
#define rd(x) scanf("%d",&(x))
#define sc scanf
#define pf printf
typedef long long ll;
const int maxn = 1e5;
const int N = 4e2+50;
const int inf = 0x3f3f3f3f;
const ll llinf= 0x3f3f3f3f3f3f3f3f;
int main()
{
int n;rd(n);
char a[N];
int len[N] = {0};
int pos[N] = {0};
sc("%s", a+1);
stack<char> s;
int poi = 0, big=0;
for(int i=1; i<=n; ++i){
if(a[i] == '['){
s.push('[');
poi+=1;
len[i] = poi;
}else{
len[i] = poi;
s.pop();
poi-=1;
}
big = max(big, len[i]);
}
char mp[N][N];
const int m = big*2+1;
int j=1;
for(int i=1; i<=n; ++i){
int w = (big-len[i]+1)*2+1;
int st = (m-w)/2+1;
for(int k=st; k<=st+w-1; ++k){
if(k==st||k==st+w-1){
mp[k][j] = '+';
if(a[i]=='[')
mp[k][j+1] = '-';
else
mp[k][j-1] = '-';
}else{
mp[k][j] = '|';
}
}
j += (a[i]=='['&&a[i+1]==']')? 4:1;
}
for(int i=1; i<=m; ++i){
for(int k=1; k<=j-1; ++k)
{
char ch = mp[i][k];
if(ch=='|'||ch=='+'||ch=='-')
putchar(ch);
else
putchar(' ');
}
if(i!=m)
puts("");
}
return 0;
}