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<bits/stdc++.h> using namespace std; typedef long long ll; map<char,int> fre; set<char> line; map<string,char> Decode; map<char,string> Code; int relnum; int sumnum; void Freq() { freopen("Text.txt","r",stdin); freopen("Freq.txt","w",stdout); char ch ; while(~scanf("%c",&ch)) { fre[ch]++; line.insert(ch); sumnum++; if(fre[ch] == 1) relnum++; } for(auto it : line) cout<<it<<" "<<fre[it]<<endl; fclose(stdin); fclose(stdout); } struct Node { int data; int parent,lchild,rchild; }; struct Num { int w; int ch; }W[1000]; void CreateNode(Node *H,int w,int parent,int lchild,int rchild) { H -> lchild = lchild; H -> rchild = rchild; H -> parent = parent; H -> data = w; } void CreateHuffmanTree(Node*& HT,int n) { HT = (Node*)malloc((2*n-1) * sizeof(Node)); for(int i=0;i<n;i++) { CreateNode(HT+i,W[i].w,-1,-1,-1); } for(int i=n;i<2*n-1;i++) { int min1 = 0x3f3f3f3f,min2 = min1; int x1 = -1 , x2 = -1; for(int j = 0;j < i;j++) { if((HT+j) -> parent == -1) { if((HT+j) -> data < min1) { min2 = min1; min1 = (HT+j) -> data; x2 = x1,x1 = j; } else if((HT+j)->data < min2) { min2 = (HT+j) -> data; x2 = j; } } } (HT+x1) -> parent = i; (HT+x2) -> parent = i; CreateNode(HT+i,min1+min2,-1,x1,x2); } } void HuffmanTreeCode(Node *HT,char *str,int n,int path,int &len) { int j,child,parent; j = 0; child = path; parent = (HT+child) -> parent; while(parent != -1) { if((HT+parent) -> lchild == child) str[j++] = '0'; else str[j++] = '1'; child = parent; parent = (HT+child) -> parent; } len = j; } void HuffmanTree() { freopen("Freq.txt","r",stdin); freopen("HaffCode.txt","w",stdout); int len; Node* HT; for(int i=0;i<relnum;i++) scanf("%c %d\n",&W[i].ch,&W[i].w); CreateHuffmanTree(HT,relnum); char str[relnum]; for(int k=0;k<relnum;k++) { HuffmanTreeCode(HT,str,relnum,k,len); string s; for(int j=len-1;j>=0;j--) s+=str[j]; Code[W[k].ch] = s; Decode[s] = W[k].ch; } for(auto it : line) cout<<it<<" : "<<Code[it]<<endl; free(HT); fclose(stdin); fclose(stdout); } int codelen; void CodeText() { freopen("Text.txt","r",stdin); freopen("EngtoCode.txt","w",stdout); char ch; while(~scanf("%c",&ch)) { cout<<Code[ch]; codelen += Code[ch].size(); } fclose(stdin); fclose(stdout); } int numnum; void DecodeText() { freopen("EngtoCode.txt","r",stdin); freopen("DecodeText.txt","w",stdout); char ch; string s; while(~scanf("%c",&ch)) { s+=ch; if(Decode[s]) { cout<<Decode[s]; s.clear(); numnum++; } } fclose(stdin); fclose(stdout); } int main() { Freq(); freopen("CON","w",stdout); cout<<"Freq.txt文件已创建"<<endl; cout<<"字符总数 : "<<sumnum<<endl; cout<<"有效字符 : "<<relnum<<endl<<endl; cout<<"--------------------"<<endl<<endl; fclose(stdout); HuffmanTree(); freopen("CON","w",stdout); cout<<"HaffCode.txt文件已创建"<<endl<<endl; cout<<"--------------------"<<endl<<endl; fclose(stdout); CodeText(); freopen("CON","w",stdout); cout<<"EngtoCode.txt文件已创建"<<endl; cout<<"编码后位数 : "<<codelen<<endl<<endl; cout<<"--------------------"<<endl<<endl; fclose(stdout); DecodeText(); freopen("CON","w",stdout); cout<<"DecodeText.txt文件已创建"<<endl; cout<<"字符总数 : "<<numnum<<endl<<endl; cout<<"--------------------"<<endl<<endl; fclose(stdout); freopen("CON","w",stdout); cout<<"压缩前位数:"<<sumnum<<" * "<<8<<" = "<<sumnum*8<<endl; cout<<"压缩后位数:"<<codelen<<endl; cout<<"压缩比:"<<codelen<<" / "<<sumnum*8<<" = "<<1.0*codelen/(sumnum*8); printf("(%.2lf",1.0*codelen/(sumnum*8)*100); cout<<"%)"<<endl<<endl; cout<<"--------------------"<<endl<<endl; fclose(stdout); return 0; }
|