1117 - return; } map<uchar,Node>::iterator iter;

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include<iostream> #include<map> #include<queue> #include<cstring> #include<cstdio> # using namespace std; typedef unsigned char uchar; t uchar buffer[1024]; int len; i struct Node { unsigned int freq:16; unsigned int deep:8; unsigned int pred:8; }; map<uchar, Node> table; m struct comp{ int operator()(uchar a, uchar b){ return table[a].freq > table[b].freq; } }; } priority_queue<uchar, vector<uchar>, comp > myque; p void compute_deep(uchar c){ Node &n = table[c]; if(n.deep) return ; if(n.pred ==0){ n.deep =1; } else { compute_deep(n.pred); n.deep = table[n.pred].deep + 1; } } int calc(){ map<uchar,Node>::iterator iter; m int ret = 0; for(iter=table.begin();iter!= table.end();iter++){ compute_deep(iter->first); if(iter->first <128 ){ Node &t = iter->second; ret += (t.deep-1) * t.freq; } } return ret; } void fun(){ int i; len = 0; table.clear(); for(i=0;buffer[i];i++){ table[buffer[i]].freq++;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
} len = i; if(table.size() < 3){ i cout<< len*8 <<" "<<len<<" "; cout.precision(1); cout<<fixed<<8.0<<endl;
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: return; } map<uchar,Node>::iterator iter; for(iter=table.begin();iter!= table.end();iter++){ myque.push(iter->first); } uchar first,second; uchar start = 128; first = myque.top(); myque.pop(); second = myque.top(); myque.pop(); table[start].freq = table[first].freq + table[second].freq; table[first].pred = start; table[second].pred = start; while(!myque.empty()){ myque.push(start); start ++; first = myque.top(); myque.pop(); second = myque.top(); myque.pop(); table[start].freq = table[first].freq + table[second].freq; table[first].pred = start; table[second].pred = start; } table[start].pred = 0; int t = calc(); int num = len * 8; cout<<num<<" "<< t <<" "; double ratio = num *1.0/t; cout.precision(1); c cout<<fixed<<ratio<<endl; } int main(){ cin>>buffer; while(strcmp("END", (const char *)buffer)!=0){ fun(); cin>>buffer; } return 0; }...
View Full Document

This note was uploaded on 02/18/2010 for the course CS 100 taught by Professor Acm during the Spring '10 term at Zhejiang University.

Page1 / 2

1117 - return; } map<uchar,Node>::iterator iter;

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online