[编程题] 字符编码
来源:互联网 发布:网络教育2.5年毕业 编辑:程序博客网 时间:2024/06/01 07:35
请设计一个算法,给一个字符串进行二进制编码,使得编码后字符串的长度最短。
输入描述:
每组数据一行,为待编码的字符串。保证字符串长度小于等于1000。
输出描述:
一行输出最短的编码后长度。
输入例子:
MT-TECH-TEAM
输出例子:
33
#include <iostream>#include <string>#include <map>#include <queue>#include <algorithm>using namespace std;bool cmp(int first,int next){ return first<next;}int main(){ string input; while(cin>>input){ if(input.empty()==true) continue; map<char,int> m; for(int i = 0;i<input.size();i++){ ++m[input[i]]; } deque<int> dq; for(map<char,int>::iterator it=m.begin();it!=m.end();it++){ dq.push_back((*it).second); } stable_sort(dq.begin(),dq.end(),cmp); int result =0; while(dq.size()>1){ int first = dq.front(); dq.pop_front(); int next = dq.front(); dq.pop_front(); result+=first+next; dq.push_back(first+next); stable_sort(dq.begin(),dq.end(),cmp); } cout<<result<<endl; } return 0;}
阅读全文
0 0
- [编程题] 字符编码
- 编程各种字符编码
- 编程(1)-字符编码
- 字符编码与编程语言
- Windows编程入门-字符编码
- 字符编码在编程中的问题
- 走进windows编程的世界-----字符编码
- 【Windows学习】Windows核心编程-字符编码
- 【java编程】字符编码之加密解密
- C#编程总结(九)字符编码
- C#编程总结(九)字符编码
- Windows核心编程 之 字符编码
- 字符编码(哈夫曼编码、贪心算法、priority_queue 的应用)---- 美团2016研发工程师编程题(二)
- 美团点评2016研发题目-[编程题-美团] 字符编码
- 字符编码
- 字符编码
- 字符编码
- 字符 编码
- android 字体颜色随机
- hdu5922Minimum’s Revenge
- opencv 图像的载入显示和输出
- Python默认参数是[]
- 1
- [编程题] 字符编码
- Python:关于OS的介绍
- HDOJ2577 DP简单入门题
- I
- 乱码处理
- 【Android】封装BroadcastReceiver
- NIO-文件读取及按行读取示例
- IDEA 热部署神器:JreBel
- nfs 文件句柄