/ tabris /

记录详情

Accepted

/in/foo.cc: In function 'void analysisStr(char*, int&, int, int)':
/in/foo.cc:23:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
         for(int i=l;i<r;i++) printf("%c",s[i]);puts("");
         ^~~
/in/foo.cc:23:48: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the 'for'
         for(int i=l;i<r;i++) printf("%c",s[i]);puts("");
                                                ^~~~
/in/foo.cc: In function 'void work()':
/in/foo.cc:133:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
         if(flag) puts("");flag = 1;
         ^~
/in/foo.cc:133:27: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the 'if'
         if(flag) puts("");flag = 1;
                           ^~~~
# 状态 耗时 内存占用
#1 Accepted 3ms 1.48 MiB
#2 Accepted 3ms 1.504 MiB
#3 Accepted 3ms 1.215 MiB
#4 Accepted 3ms 1.215 MiB
#5 Accepted 2ms 1.211 MiB
#6 Accepted 3ms 1.203 MiB
#7 Accepted 2ms 1.215 MiB
#8 Accepted 2ms 1.215 MiB
#9 Accepted 2ms 1.211 MiB
#10 Accepted 2ms 1.203 MiB

代码

#include <bits/stdc++.h>
typedef long long int LL;
using namespace std;

const int N = 1e5+7;
const int MOD = 1e9+7;

#define DEBUG false

int tree[100][2],cnt;
int split[100];
int rectangle[100][2];

char s[N];
int ans[505][505];

// i think this problem should use DFS 
// @params:l,r => 当前字符串的范围[l, r)
// @params:f 当前的待处理节点
void analysisStr(char *s, int &rt, int l, int r){
    if(DEBUG) {
        printf(">>> rt [%d,%d)|  ", l, r);
        for(int i=l;i<r;i++) printf("%c",s[i]);puts("");
    }
    int p = l;
    // NxM
    int n, m;n = m = 0;
    for(;p<r;p++){
        if(s[p] == '[' || s[p] == 'x' || s[p] == ']' || s[p] == '\0') break;
        n = n*10 +(s[p]-'0');
    }
    p++; // 把中断的遍历
    for(;p<r;p++){
        if(s[p] == '[' || s[p] == 'x' || s[p] == ']' || s[p] == '\0') break;
        m = m*10 +(s[p]-'0');
    }

    // 把当前节点更新到树上去
    rt = ++cnt;
    rectangle[cnt][0] = n;
    rectangle[cnt][1] = m;

    // 如果没有[]的内容就return
    if(DEBUG) printf("p=%d s[p]=%c <<<<< \n", p, s[p]);
    if(s[p] != '[') return;
    if(DEBUG) puts(" ----- ");

    // [ | or -,
    p++; // 遍历过 "["
    if(s[p] == '-') split[cnt] = 1;
    else            split[cnt] = 2;
    p++,p++; // 遍历过 "| or -", ","
    // [,***,]
    if(DEBUG) printf("p=%d s[p]=%c ===\n",p,s[p]);
    int dfsl = p, num = 0; // 深搜的开始l,num记录括号数量。
    for(;p<r;p++){
        if(s[p] == '[') ++num;
        if((s[p] == ',' || s[p] == ']')&&0==num) {
            analysisStr(s, tree[rt][0], dfsl, p);
            break;
        }
        if(s[p] == ']') --num;
    }

    // [, , ***]
    p++; // 遍历过 ","
    dfsl = p, num = 0; // 深搜的开始l,num记录括号数量。
    for(;p<r;p++){
        if(s[p] == '[') ++num;
        if((s[p] == ',' || s[p] == ']')&&0==num) {
            analysisStr(s, tree[rt][1], dfsl, p);
            break;
        }
        if(s[p] == ']') --num;
    }

}

// 左上角为(x,y)时 长宽为(n,m)
void paint(int n, int m, int x, int y){
    // |
    for(int i=x;i<x+n;i++) ans[i][y] |= 1;
    for(int i=x;i<x+n;i++) ans[i][y+m-1] |= 1;

    // -
    for(int j=y;j<y+m;j++) ans[x][j] |= 2;
    for(int j=y;j<y+m;j++) ans[x+n-1][j] |= 2;
}

void dfs(int u,int x,int y){
    if(0 == u) return ;

    paint(rectangle[u][0],rectangle[u][1],x,y);

    if(split[u] == 1){ // -
        int tmp = rectangle[tree[u][0]][0];
        dfs(tree[u][0],x,y);
        dfs(tree[u][1],x+tmp-1,y);
    }
    else { // |
        int tmp = rectangle[tree[u][0]][1];
        dfs(tree[u][0],x,y);
        dfs(tree[u][1],x,y+tmp-1);
    }
}

void init(){
    cnt = 0;
    memset(tree,0,sizeof(tree));
    memset(split,0,sizeof(split));
    memset(rectangle,0,sizeof(rectangle));
    memset(ans,0,sizeof(ans));
}

void output(){
    int n = rectangle[1][0];
    int m = rectangle[1][1];

    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(1 == ans[i][j]) printf("|");
            else if(2 == ans[i][j]) printf("-");
            else if(3 == ans[i][j]) printf("+");
            else printf(" ");
        }
        puts("");
    }
}

void work(){
    int flag = 0;
    while(~scanf("%s",s)){
        if(flag) puts("");flag = 1;
        init();
        int rt = 0;
        analysisStr(s, rt, 0, strlen(s));
        if(DEBUG) puts("^^^^^^^^^^^^^^^^");
        dfs(1,1,1);
        if(DEBUG){
            for(int i=1;i<=cnt;i++){
                printf("[%d %d %d] (split:%d) (NxM:%d %d)\n",i,tree[i][0],tree[i][1],split[i],rectangle[i][0],rectangle[i][1]);
            }
        }
        output();
    }
}

int main(){
    work();
    return 0;
}

信息

递交者
类型
递交
语言
C++
递交时间
2019-06-10 11:53:26
评测时间
2019-06-10 11:53:26
评测机
分数
100
总耗时
30ms
峰值内存
1.504 MiB