📄 2274.cpp
字号:
/* This Code is Submitted by wywcgs for Problem 2274 on 2006-06-14 at 11:36:24 */
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
class Tree {
public:
char name[10];
int f, sp, cl, cr, cb, ce;
vector<Tree*> chd;
Tree(int, int);
~Tree() { int i; for(i = 0; i < (int)chd.size(); i++) delete chd[i]; }
void show() const { printf("[%-8s]", name); }
};
Tree::Tree(int fn, int l) : f(fn) {
scanf("%[a-zA-Z0-9]s", name); chd.clear();
int prev = l;
while(getchar() != ')') {
getchar();
chd.push_back(new Tree(f+1, prev));
prev = chd.back()->ce;
}
if(chd.empty()) { cb = sp = cl = prev; ce = cr = prev+10; }
else {
cl = chd.front()->sp; cr = chd.back()->sp+10;
cb = chd.front()->cb; ce = chd.back()->ce; sp = (cb+ce)/20*10;
}
}
int main()
{
char c;
while((c = getchar()) != EOF) {
Tree *root = new Tree(0, 0);
c = getchar();
queue<Tree*> Q1, Q2;
Q1.push(root);
int cf = -1, cp, i;
while(!Q1.empty()) {
Tree *t = Q1.front(); Q1.pop();
Q2.push(t);
if(t->f != cf) {
cp = 0; if(cf >= 0) putchar('\n');
while(Q2.front() != t) {
Tree *p = Q2.front(); Q2.pop();
if(p->chd.empty()) continue;
for(; cp < p->cr-1; cp++) putchar(cp <= p->cl ? ' ' : '-');
}
cp = 0; if(cf >= 0) putchar('\n');
cf = t->f;
}
for(; cp < t->sp; cp++) putchar(' ');
t->show(); cp += 10;
for(i = 0; i < (int)t->chd.size(); i++) Q1.push(t->chd[i]);
}
printf("\n\n");
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -