Codeforces 770D Draw Brackets!【模拟+思维】

D. Draw Brackets!
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A sequence of square brackets is regular if by inserting symbols "+" and "1" into it, you can get a regular mathematical expression from it. For example, sequences "[[]][]", "[]" and "[[][[]]]" — are regular, at the same time "][", "[[]" and "[[]]][" — are irregular.

Draw the given sequence using a minimalistic pseudographics in the strip of the lowest possible height — use symbols '+', '-' and '|'. For example, the sequence "[[][]][]" should be represented as:

+-        -++- -+    
|+- -++- -+||   |
||   ||   |||   |
|+- -++- -+||   |
+-        -++- -+

Each bracket should be represented with the hepl of one or more symbols '|' (the vertical part) and symbols '+' and '-' as on the example which is given above.

Brackets should be drawn without spaces one by one, only dividing pairs of consecutive pairwise brackets with a single-space bar (so that the two brackets do not visually merge into one symbol). The image should have the minimum possible height.

The enclosed bracket is always smaller than the surrounding bracket, but each bracket separately strives to maximize the height of the image. So the pair of final brackets in the example above occupies the entire height of the image.

Study carefully the examples below, they adequately explain the condition of the problem. Pay attention that in this problem the answer (the image) is unique.

Input

The first line contains an even integer n (2 ≤ n ≤ 100) — the length of the sequence of brackets.

The second line contains the sequence of brackets — these are n symbols "[" and "]". It is guaranteed that the given sequence of brackets is regular.

Output

Print the drawn bracket sequence in the format which is given in the condition. Don't print extra (unnecessary) spaces.

Examples
Input
8
[[][]][]
Output
+-        -++- -+
|+- -++- -+||   |
||   ||   |||   |
|+- -++- -+||   |
+-        -++- -+
Input
6
[[[]]]
Output
+-     -+
|+-   -+|
||+- -+||
|||   |||
||+- -+||
|+-   -+|
+-     -+
Input
6
[[][]]
Output
+-        -+
|+- -++- -+|
||   ||   ||
|+- -++- -+|
+-        -+
Input
2
[]
Output
+- -+
|   |
+- -+
Input
4
[][]
Output
+- -++- -+
|   ||   |
+- -++- -+

题目大意:

按照形状输出即可。


思路:


模拟题缕清要进行模拟的步骤一点一点谨慎点写就行。

首先处理出来一个前缀和,sum【i】表示到第i个位子,左括号比右括号多的数量。

那么很容易理解,对于两个匹配的括号i,j,就是sum【i】后边,第一个sum【i】-1的位子j,

那么我们再处理出来两个数组,marry【i】表示第i个括号匹配的编号,那么如果有marry【i】==marry【j】,表示i,j两个括号是匹配的。

再处理出来一个数组size【i】,表示第i个括号的大小。

那么处理好之后,剩下部分模拟即可。


Ac代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
struct node
{
    int l,r;
}b[150000];
int marry[15000];
int size[15000];
int num[15000];
char ans[150][10000];
char a[15000];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        scanf("%s",a+1);
        int cnt=0;
        int maxn=0;
        int sum=0;
        int l=1;
        memset(ans,' ',sizeof(ans));
        for(int i=1;i<=n;i++)
        {
            if(a[i]=='[')sum++;
            else sum--;
            maxn=max(maxn,sum);
            if(sum==0)
            {
                b[cnt].l=l;
                b[cnt].r=i;
                cnt++;
                l=i+1;
            }
            num[i]=sum;
        }
        for(int i=0;i<cnt;i++)
        {
            int biaoji=1;
            int tmpsize=maxn*2+1;
            for(int j=b[i].l;j<=b[i].r;j++)
            {
                if(a[j]=='[')
                {
                    for(int k=j;k<=b[i].r;k++)
                    {
                        if(num[j]==num[k]+1)
                        {
                            marry[j]=marry[k]=biaoji++;
                            size[j]=size[k]=tmpsize;
                            tmpsize-=2;
                            size[j]/=2;size[k]/=2;
                            break;
                        }
                    }
                }
                else tmpsize+=2;
            }
        }
        int pos=0;
        for(int i=1;i<=n;i++)
        {
            if(a[i]=='[')
            {
                for(int j=1;j<=maxn*2+1;j++)
                {
                    int mid=(maxn*2+1)/2+1;
                    if(j<mid-size[i]||j>mid+size[i])
                    {
                        if(ans[j][pos]=='-')continue;
                        else ans[j][pos]=' ';
                    }
                    else if(j==mid-size[i]||j==mid+size[i])ans[j][pos]='+';
                    else ans[j][pos]='|';
                }
                pos++;
                for(int j=1;j<=maxn*2+1;j++)
                {
                    if(ans[j][pos-1]=='+')ans[j][pos]='-';
                    else ans[j][pos]=' ';
                }
                if(marry[i]==marry[i+1])
                {
                    pos++;
                    for(int j=1;j<=maxn*2+1;j++)
                    {
                        ans[j][pos]=' ';
                    }
                    pos++;
                }
            }
            else
            {
                pos++;
                for(int j=1;j<=maxn*2+1;j++)
                {
                    int mid=(maxn*2+1)/2+1;
                    if(j<mid-size[i]||j>mid+size[i])
                    {
                        if(ans[j][pos]=='-')continue;
                        else ans[j][pos]=' ';
                    }
                    else if(j==mid-size[i]||j==mid+size[i])ans[j][pos]='+';
                    else ans[j][pos]='|';
                }
                pos--;
                for(int j=1;j<=maxn*2+1;j++)
                {
                    if(ans[j][pos+1]=='+')ans[j][pos]='-';
                }
                pos++;
                if(i==n||a[i+1]=='[')pos++;
            }
        }
        for(int i=1;i<=maxn*2+1;i++)
        {
            for(int j=0;j<pos;j++)
            {
                printf("%c",ans[i][j]);
            }
            printf("\n");
        }
    }
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值