Advertisement
saske_7

huffman_lab.cpp

Jan 3rd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define bound(x,y , i , j) x >= 0 && x < i && y >= 0 && y < j
  6. #define mp make_pair
  7. #define pb push_back
  8. #define pii pair<int , int>
  9. #define M 205
  10. #define rep(i , n) for(i = 0 ; i< n ;i++ )
  11. #define repi(i , n ) for(i = 0 ; i<= n ; i++ )
  12. #define mem(x , y ) memset(x , y , sizeof x )
  13. #define mx 5000000
  14. #define ff first
  15. #define ss second
  16.  
  17. //int dx[] = { -1 ,+1, 0 , 0 , -1 , -1  , 1 , 1 };
  18. //int dy[] = { 0 , 0, +1, -1  , +1 , -1 , 1 , -1 };
  19.  
  20. //int dx[] = { 1 , 1, 2 , 2  , -1 , -1 , -2 , -2 }; // knight direction
  21. //int dy[] = { 2 , -2, +1, -1 ,2 , -2 , 1 , -1 };
  22.  
  23.  
  24. struct myClass{
  25.     char a ;
  26.     int f ;
  27.     myClass(){ f =  -1 ; }
  28.     myClass(char a , int f ){
  29.         this -> a = a ;
  30.         this -> f = f ;
  31.     }
  32.     bool operator<(myClass x) const{ return this-> f > x.f } ;
  33. };
  34.  
  35.  
  36. vector<myClass > v ;
  37. vector< int > g[M] , cost[M] ;
  38.  
  39. map< char , int > m ;
  40. map<myClass , int > track  ;
  41.  
  42. priority_queue< myClass > pq ;
  43.  
  44. int par[M]  , vis[M];
  45.  
  46. void clr(){
  47. int i ;
  48.     rep(i , M ) g[i].clear() ;
  49.     rep(i , M ) cost[i].clear() ;
  50.     rep(i , M) par[i] = i  ;
  51.     if(!pq.empty()) while(!pq.empty() ) pq.pop() ;
  52.     mem(vis , 0 );
  53.     m.clear() ;
  54.     track.clear() ;
  55. }
  56.  
  57. void dfs(int u ){
  58.     int i ;
  59.  
  60.     rep(i , g[u].size() ){
  61.         int v =  g[u][i] ;
  62.         if(vis[v ] == -1  ){
  63.             vis[v] = 1 ;
  64.             par[v] = u ;
  65.             dfs( v) ;
  66.         }
  67.  
  68.     }
  69.  
  70.  
  71. }
  72.  
  73. void huffman(int c ){
  74.  
  75. int i ,j , k = 0   , var ;
  76.      c = 0 ;
  77.     while(1){
  78.         k=  0 ;
  79.         myClass x =  pq.top() ;
  80.  
  81.         if(track.find(x) ==  track.end() ) {
  82.             track[x] =  c++ ;
  83.             if(x.f ==  -1 ) m[x.a] = track[x] ;
  84.  
  85.         }
  86.  
  87.         pq.pop() ;
  88.         while(!pq.empty()){
  89.             k =  1;
  90.             myClass y =  pq.top() ;  pq.pop() ;
  91.  
  92.             if(track.find(y) ==  track.end() ) {
  93.                 track[y] =  c++ ;
  94.                 if(x.f ==  -1 ) m[y.a] = c++;
  95.  
  96.             }
  97.  
  98.             int u =  m[x] ;
  99.             int v =  m[y] ;
  100.            
  101.             myClass node ;
  102.             track[node] = c++ ;
  103.             node.f = x.f + y.f ;
  104.             pq.push(node) ;
  105.            
  106.             if(x.f < y.f ){
  107.                 g[node].pb( x) ; cost[node].pb(0) ;
  108.                 g[node].pb( y) ; cost[node].pb(1) ;
  109.  
  110.             }
  111.             else {
  112.                 g[node].pb( x) ; cost[node].pb(1) ;
  113.                 g[node].pb( y) ; cost[node].pb(0) ;
  114.  
  115.             }
  116.  
  117.  
  118.         }
  119.  
  120.         if(k ==  0 ) break ;
  121.     }
  122.  
  123.     --c ;
  124.     dfs(c) ;
  125.    
  126. }
  127.  
  128. int main(){
  129. //  freopen("in.txt", "r",stdin );
  130.  
  131.     int i , j , k , cs= 0 , tc  , c = 0 ;
  132.  
  133.     cin >> tc  ;
  134.     while(tc-- ){
  135.  
  136.         char s[10] ;
  137.         scanf("%s %d" , s , &k );
  138.         printf("%c %d\n", s[0] , k ) ;
  139.  
  140.        
  141.         pq.push( myClass(s[0], k)  );
  142.  
  143.     }
  144.     huffman(c ) ;
  145.  
  146.     rep(i , n ) printf("%d ", find(i) );
  147.  
  148.     return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement