poj1057

简单题

View Code
#include <iostream>
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
#include
<algorithm>
using namespace std;

int dcount;
int n;
char word[1005][50];
int father[1005];
int fa;
int dir[1005][1005];
int num[1005];
int id[1005];

void ins(char *st)
{
if (strcmp(st, "]") == 0)
{
fa
= father[fa];
return;
}
if (st[0] == 'f')
{
dir[fa][num[fa]
++] = n;
return;
}
father[dcount]
= fa;
id[dcount]
= n;
fa
= dcount++;
}

bool cmp(const int &a, const int &b)
{
return strcmp(word[a], word[b]) < 0;
}

void print(int a, int d)
{
for (int i = 0; i < a; i++)
printf(
"| ");
printf(
"%s\n", word[id[d]]);
for (int i = d + 1; i < dcount; i++)
if (father[i] == d)
print(a
+ 1, i);
for (int i = 0; i < num[d]; i++)
{
for (int j = 0; j < a; j++)
printf(
"| ");
printf(
"%s\n", word[dir[d][i]]);
}
}

int main()
{
//freopen("t.txt", "r", stdin);
strcpy(word[0], "ROOT");
int t = 0;
while (scanf("%s", word[1]), strcmp("#", word[1]) != 0)
{
t
++;
memset(num,
0, sizeof(num));
dcount
= 1;
n
= 1;
fa
= 0;
father[
0] = -1;
ins(word[n]);
while (scanf("%s", word[++n]), strcmp("*", word[n]) != 0)
ins(word[n]);
for (int i = 0; i < dcount; i++)
sort(dir[i], dir[i]
+ num[i], cmp);
printf(
"DATA SET %d:\n", t);
print(
0, 0);
putchar(
'\n');
}
return 0;
}

你可能感兴趣的:(poj)