欢迎使用本站,预祝练习时长两年半的选手们到成功! [本模块信息来自tem/def/head]

whj rhx 考前复习

时间:2024-06-23 15:18 作者:admin 点击:
freopen("xx.in","r",stdin); freopen("xx.out","w",stdout);//a 追加写入 int a,b; cinab; couta+b; fclose(stdin); fclose(stdout); ifstream fin("xx.in"); ofstream fout("xx.out");//("xx.out",ios::app); 追加写入 int a,b; finab; fouta+b; fi

freopen("xx.in","r",stdin);

freopen("xx.out","w",stdout);//a 追加写入

int a,b;

cin>>a>>b;

cout<<a+b;

fclose(stdin);

fclose(stdout);


ifstream fin("xx.in");

ofstream fout("xx.out");//("xx.out",ios::app); 追加写入

int a,b;

fin>>a>>b;

fout<<a+b;

fin.close();

fout.close();


哈夫曼树:数据压缩

1、统计字符出现的频率

2、将<字符,频率>存入堆中

3、反复取两次堆顶,合并,放回,直到剩下最后一个节点

string s="ababccccda";

struct HuffmanNode{

   char c;

   int pl;

   HuffmanNode *l,*r;

   HuffmanNode(char a,int b){

       c=a;

       pl=b;

       l=nullptr;

       r=nullptr;

   }

};

struct cmp{

   bool operator()(HuffmanNode *a,HuffmanNode *b){

       return a->pl>b->pl;

   }

};

priority_queue<HuffmanNode*,vector<HuffmanNode*>,cmp> pq;//从大到小->小顶堆


HuffmanNode* buildHuffmanTree(){

   while(pq.size()>1){

       HuffmanNode *a=pq.top();

       pq.pop();

       HuffmanNode *b=pq.top();

       pq.pop();

       HuffmanNode *c=new HuffmanNode('@',a->pl+b->pl);

       c->l=a;

       c->r=b;

       pq.push(c);

   }

   return pq.top();

}


void dfs(HuffmanNode *p,string hfcode){

   if(p->c!='@'){

       cout<<p->c<<":"<<hfcode<<endl;

       return ;

   }

   if(p->l!=nullptr) dfs(p->l,hfcode+'0');

   if(p->r!=nullptr) dfs(p->r,hfcode+'1');

}


string s="ababccccda";

map<char,int> m;

for(int i=0;s[i];i++) m[s[i]]++;

for(auto i=m.begin();i!=m.end();i++){

   HuffmanNode *p=new HuffmanNode(m->first,m->second);

   pq.push(p);

}

HuffmanNode *root=buildHuffmanTree();

dfs(root,"");



struct SearchNode{

   int val;

   SearchNode *l,*r;

   SearchNode(int a){

       val=a;

       l=r=nullptr;

   }

};

int a[10]={8,3,6,54,7,3,44,2,6,57};

void insertNode(SearchNode *p,int val){

   if(p==nullptr)

       p=new SearchNode(val);

   else

       if(val>p->val) insertNode(p->r,val);

       else insertNode(p->l,val);

}


先序遍历+中序遍历->后序

void hx(string xx,string zx){

   char root=xx[0];

   int d=zx.find(root);

   string xl=xx.substr(1,d);

   string xr=xx.substr(d+1);

   string zl=zx.substr(0,d);

   string zr=zx.substr(d+1);

   //后序遍历:左右根

   hx(xl,zl);

   hx(xr,zr);

   cout<<root;

}


(责任编辑:admin)
    顶一下
    (0)
    0%
    踩一下
    (0)
    0%