实验三——贪心算法·哈夫曼编码
2021-03-10 11:29
标签:+= main rev print using _for ios type col pq;
int main()
{
printf("输入小写字母总数:");
scanf("%d",&n);
printf("输入小写字母和频率:\n");
_for(i,1,n+1)
scanf(" %c%d",&s[i],&a[i]);
_for(i,1,n+1)
pq.push({a[i],vectorchar>(1,s[i])});
while(pq.size()!=1)
{
p a1 = pq.top();
pq.pop();
p a2 = pq.top();
pq.pop();
p a3;
a3.val = a1.val + a2.val;
_for(i,0,a1.nd.size())
{
rnt[a1.nd[i]-‘a‘] += ‘1‘;
a3.nd.pb(a1.nd[i]);
}
_for(i,0,a2.nd.size())
{
rnt[a2.nd[i]-‘a‘] += ‘0‘;
a3.nd.pb(a2.nd[i]);
}
pq.push(a3);
}
_for(i,0,30)
reverse(rnt[i].begin(),rnt[i].end());
// _for(i,0,30)
// while(rnt[i].size() && rnt[i][rnt[i].size()-1]==‘0‘)
// rnt[i].pop_back();
printf("小写字母及其对应哈夫曼编码:\n");
_for(i,0,30)
if(rnt[i].size())
printf("%c %s\n",‘a‘+i,rnt[i].c_str());
return 0;
} 实验三——贪心算法·哈夫曼编码 标签:+= main rev print using _for ios type col 原文地址:https://www.cnblogs.com/Asurudo/p/14153594.html/*Hatsune Miku 4ever!*/
#include
上一篇:Java 异常
文章标题:实验三——贪心算法·哈夫曼编码
文章链接:http://soscw.com/index.php/essay/62741.html