正文

Zju 1097 Code the Tree2006-12-17 23:23:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/cruxd/21676.html

分享到:

2173944 2006-12-17 23:15:34 Accepted 1097 C++ 00:00.02 876K Crux.D

    烦题,输入巨无聊。如果不想自己写的话参考一下吧。

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

string s;
int a[51][51], b[51], c[51], si, mx;

int node()
{
 int t = 0;
 while(s[si] >= '0' && s[si] <= '9')
 {
  t *= 10;
  t += s[si ++] - '0';
 }
 return t;
}

void build(int n)
{
 int t;
 if(n > mx) mx = n;
 while(s[si] == '(')
 {
  si ++;
  t = node();  
  if(n)
  {
   b[n] ++;
   b[t] ++;
   a[t][n] = 1;
   a[n][t] = 1;
  }
  build(t);
  si ++;
 }
}

void init()
{
 si = 0, mx = 0;
 memset(b, 0, sizeof(b));
 memset(a, 0, sizeof(a));
 memset(c, 0, sizeof(c));
 int i;
 string ts = "";
 for(i = 0; i < s.size(); i ++)
 {
  if(s[i] != ' ')
   ts += s[i];
 }
 s = ts;
}

void print()
{
 int i, k, j, min, v;
 //pb();
 for(i = 0; i < mx - 1; i ++)
 {
  if(i) printf(" ");
  min = mx;
  for(j = 1; j <= mx; j ++)
  {
   if(!c[j] && b[j] == 1 && min > j)
    min = j;
  }
  for(k = 1; k <= mx; k ++)
  {
   if(a[min][k] || a[k][min])
   {
    v = k; break;
   }
  }
  printf("%d", v);
  a[min][v] = 0, a[v][min] = 0;
  b[v] --;
  c[min] = 1;
  b[min] = 0;
  //pa();
  //pb();
 } 
 printf("\n");
}

int main()
{
 //freopen("in.txt", "r", stdin);
 while(getline(cin, s))
 {
  init();
  build(0);
  //pa();
  print();
 }
 return 0;
}

阅读(3246) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册