1 #include<iostream> 2 #include<algorithm> 3 #include<cstdlib> 4 #include<cstring> 5 6 #define LCHILD ‘0‘ 7 #define RCHILD ‘1‘ 8 9 using namespace std; 10 11 char ans[1000][50]={0}; 12 char temp[50]; 13 int n,level; 14 char a[1000]; 15 int te; 16 17 struct PriorNode 18 { 19 20 char data; 21 22 int PriorLevel; 23 24 struct PriorNode* Lchild; 25 struct PriorNode* Rchild; 26 27 }QueNode[1000]; 28 29 bool cmp(struct PriorNode a,struct PriorNode b) 30 { 31 32 return a.PriorLevel<b.PriorLevel; 33 34 } 35 36 void GetAns(PriorNode* root) 37 { 38 39 int i; 40 41 if(root==NULL) return ; 42 43 if(root->Lchild==NULL&&root->Rchild==NULL) 44 { 45 46 for(i=0;i<n;i++) 47 { 48 49 if(a[i]==root->data) { strcpy(ans[i],temp); return;} 50 51 } 52 53 } 54 55 temp[te]=LCHILD; 56 temp[te+1]=0; 57 te++; 58 59 GetAns(root->Lchild); 60 61 te--; 62 temp[te]=RCHILD; 63 temp[te+1]=0; 64 te++; 65 66 GetAns(root->Rchild); 67 68 te--; 69 temp[te]=0; 70 71 } 72 73 int main() 74 { 75 76 cin>>n>>a; 77 int i; 78 79 for(i=0;i<n;i++) 80 { 81 82 cin>>level; 83 84 QueNode[i].data=a[i]; 85 86 QueNode[i].PriorLevel=level; 87 88 QueNode[i].Lchild=NULL; 89 QueNode[i].Rchild=NULL; 90 91 } 92 93 int head,tail; 94 95 head=0,tail=n; 96 97 while(head+1<tail){ 98 99 sort(QueNode+head,QueNode+tail,cmp); 100 101 QueNode[tail].PriorLevel=QueNode[head].PriorLevel+QueNode[head+1].PriorLevel; 102 QueNode[tail].Lchild=&QueNode[head]; 103 QueNode[tail].Rchild=&QueNode[head+1]; 104 105 head+=2; 106 tail++; 107 108 } 109 110 GetAns(&QueNode[head]); 111 112 for(i=0;i<n-1;i++) 113 { 114 115 cout<<ans[i]<<endl; 116 117 } 118 119 cout<<ans[i]; 120 121 return 0; 122 123 }
题目:http://acm.swust.edu.cn/contest/0208/problem/985/
时间: 2024-11-25 08:24:54