1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| #include<iostream> #include<string> #define ok 1 #define error -1 using namespace std; const int maxW = 9999; class HuffNode{ public: int weight; int parent; int lChild; int rChild; char value; };
class HuffMan{ void MakeTree(); void SelectMin(int pos, int *s1, int *s2); public: int len; int lNum; HuffNode *huffTree; string *huffCode; void MakeTree(int n, int *wt, char *str); void Coding(); int Decode(const string codestr, char txtStr[]); void Destroy();
};
void HuffMan::MakeTree(int n, int *wt, char *str){ int i; lNum = n; len = 2*n-1; huffTree = new HuffNode[2*n]; huffCode = new string[lNum+1]; for( i=1; i<=n; i++){ huffTree[i].weight = wt[i-1]; huffTree[i].value = str[i-1]; } for( i=1; i<=len; i++){ if( i>n ){ huffTree[i].weight = 0; } huffTree[i].parent = 0; huffTree[i].lChild = 0; huffTree[i].rChild = 0; } MakeTree(); }
void HuffMan::MakeTree() { int i, s1, s2; for( i=lNum+1; i<=len; i++){ SelectMin(i-1, &s1, &s2); huffTree[s1].parent = i; huffTree[s2].parent = i; huffTree[i].lChild = s1; huffTree[i].rChild = s2; huffTree[i].weight = huffTree[s1].weight + huffTree[s2].weight; } }
void HuffMan::SelectMin(int pos, int *s1, int *s2) { int w1, w2, i; w1 = w2 = maxW; *s1 = *s2 = 0; for( i=1; i<=pos; i++){ if( w1>huffTree[i].weight && huffTree[i].parent==0 ){ w2 = w1; *s2 = *s1; w1 = huffTree[i].weight; *s1 = i; }else if( w2>huffTree[i].weight && huffTree[i].parent==0 ){ w2 = huffTree[i].weight; *s2 = i; } } }
void HuffMan::Coding() { char *cd; int i,c,f,start; cd = new char[lNum]; cd[lNum-1] = '\0'; for( i=1; i<=lNum; i++){ start = lNum - 1; for( c=i, f=huffTree[i].parent; f!=0; f=huffTree[f].parent){ if(huffTree[f].lChild == c){ cd[--start] = '0'; }else{ cd[--start] = '1'; } } huffCode[i].assign(&cd[start]); } delete []cd; }
int HuffMan::Decode(const string codestr, char *txtStr) { int i, k, c; char ch; c = len; k = 0; for(i=0; i<codestr.length(); i++){ ch = codestr[i]; if( ch=='0' ){ c=huffTree[c].lChild; }else if( ch=='1' ){ c=huffTree[c].rChild; }else{ return error; } if( !huffTree[c].lChild && !huffTree[c].rChild ){ txtStr[k] = huffTree[c].value; k++; c=len; }else{ ch = '\0'; } } if( ch=='\0' ){ return error; }else{ txtStr[k] = '\0'; } return ok; }
void HuffMan::Destroy() { len = 0; lNum = 0; delete []huffTree; delete []huffCode; }
int main(){ int t,n,i,j; int wt[800]; char ch[800]; HuffMan myHuff; cin >> t; for( i=0; i<t; i++){ cin >> n; for( j=0; j<n; j++){ cin >> wt[j]; } for( j=0; j<n; j++){ cin >> ch[j]; } myHuff.MakeTree(n,wt, ch); int k; cin >> k; while( k-- ){ string str; char txt[800]; cin >> str; if( myHuff.Decode(str,txt)!=-1){ cout << txt << endl; }else{ cout << "error" << endl; } } myHuff.Destroy(); } return 0; }
|