#include <iostream> #include <stack> using namespace std; struct node { int parent; int child[51]; int du; }nd[51]; void doRun() { int i,j,k,len,temp,root; char s[256],b[2]; stack<int> sp; int p[51]; while(gets(s)!=NULL) { for(i=0;i<51;i++) { nd[i].du = 1; nd[i].parent = 0; for(j=0;j<51;j++) nd[i].child[j] = 0; } len = 0; //计算父亲节点和度 sp.push((int)'('); for(i=1;i<(int)strlen(s);i++) { if(isdigit(s[i])) { len++; j=0; while(s[i]!=' '&& s[i]!=')') { b[j++] = s[i]; i++; } i--; b[j] = '\0'; temp = atoi(b); sp.push(temp); if(i==1) { root = temp; nd[root].du--; } } else if(s[i] == '(') { nd[sp.top()].du++; sp.push((int)'('); } else if(s[i] == ')') { while(sp.top()!=(int)'(') { temp = sp.top(); sp.pop(); } sp.pop(); if(!sp.empty()) { nd[temp].parent = sp.top(); } else { nd[temp].parent = 0; } } else { continue; } } if(len <= 1) { if(len==1) cout<<endl; continue; }

评论