标签:+= main rev print using _for ios type col
/*Hatsune Miku 4ever!*/ #include <bits/stdc++.h> using namespace std; typedef long long ll; #define _for(i,a,b) for(int i = (a);i < b;i ++) #define _rep(i,a,b) for(int i = (a);i > b;i --) #define INF 0x3f3f3f3f3f3f3f3f #define lowbit(x) ((x)&(-x)) #define pb push_back #define MIKU 39 #define Design ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) #define debug() printf("Miku Check OK!\n") #define bigmiku 3939393939 #define maxn 100039 #define maxe 1000003 struct p { int val; vector<char> nd; bool operator < (p b) const { return val > b.val; } }; int n; char s[30]; string rnt[30]; int a[30]; priority_queue<p> 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],vector<char>(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