Cod sursa(job #2679236)

Utilizator Botzki17Botocan Cristian-Alexandru Botzki17 Data 29 noiembrie 2020 23:57:18
Problema Coduri Huffman Scor 65
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.94 kb
#include <fstream>
#include <queue>
#include <vector>
#include <utility>
#include <stack>


using namespace std;
ifstream fin("huffman.in");
ofstream fout("huffman.out");

struct Node{
    int st_index, dr_index, index;
    long long int val;
    int index_litera;
    int index_parent;
};
queue<Node>q1;
queue<Node>q2;
vector<Node>nodes;

pair<Node, Node> take_nodes()
{
   Node n1, n2;
   int t =2;
   vector<Node>b;
   while(t--){

    if(!q1.empty() && !q2.empty())
    {
       if(q1.front().val >= q2.front().val)
       {
          n1 = q2.front();
          q2.pop();
          b.push_back(n1);
       }
       else{
          n1 = q1.front();
          q1.pop();
          b.push_back(n1);
       }
       continue;
    }
    if(q1.empty())
    {
        n1 = q2.front();
        q2.pop();
        b.push_back(n1);
        continue;
    }
    if(q2.empty())
    {
        n1 = q1.front();
        q1.pop();
        b.push_back(n1);
        continue;
    }

   }
   n1 = b[0];
   n2 = b[1];
   return make_pair(n1, n2);

}

void huffman(int k)
{
  while(q1.size() + q2.size()!= 1)
  {
      Node n1;
      Node n2;
      pair <Node, Node> pi = take_nodes();
      n1 = pi.first;
      n2 = pi.second;
      Node n3;
      n3.dr_index = n1.index;
      n3.st_index = n2.index;
      n3.index = k;
      k++;
      n3.val = n1.val + n2.val;
      n3.index_litera = -1;
      n3.index_parent = -1;
      nodes.push_back(n3);
      nodes[n1.index].index_parent = n3.index;
      nodes[n2.index].index_parent = n3.index;
      q2.push(n3);
  }
}


vector<pair<int, int> >sol;
int convertor(string s)
{
   int sol = 0;
   int pow = 1;
   for(int i =s.size()-1; i>=0; i--)
   {
       int x = (s[i]-'0');
       sol = sol + (x * pow);
       pow = pow * 2;
   }
   return sol;
}

void dfs(int index, int depth, long long int x)
{
   if(nodes[index].index_litera !=-1)
   {
      sol[nodes[index].index_litera].first = depth;
      sol[nodes[index].index_litera].second = x;
      return;
   }
   else{
      dfs(nodes[index].dr_index, depth + 1, ((x<<1)|1));
      dfs(nodes[index].st_index, depth + 1, ((x<<1)|0));
   }
}

int main()
{
    int n, i, j, k, x;
    Node aux;
    aux.dr_index = -1;
    aux.st_index = -1;
    aux.index_parent = -1;

    fin>>n;
    k = 0;
    for(i=0;i<n;i++)
    {
        fin>>x;
        aux.index = k;
        k++;
        aux.val = x;
        aux.index_litera = i;
        nodes.push_back(aux);
        q1.push(aux);
        sol.push_back(make_pair(0, 0));
    }
    int ck = k;
    huffman(k);
    long long sum = 0;
    for(i = ck; i <nodes.size(); i++)
        sum = sum + nodes[i].val;
    fout<<sum<<"\n";
    //for(i=0;i<n; i++)
      //  sol[i].first = dfs(int i);
    dfs(nodes.size()-1, 0, 0);
    for(i =0; i < sol.size(); i++)
    {
       fout<<sol[i].first <<" "<<sol[i].second<<"\n";
    }


    return 0;
}