MT7. 字符编码
描述
输入描述
每组数据一行,为待编码的字符串。保证字符串长度小于等于1000。输出描述
一行输出最短的编码后长度。示例1
输入:
MT-TECH-TEAM
输出:
33
C++ 解法, 执行用时: 1ms, 内存消耗: 368KB, 提交时间: 2017-08-31
#include<iostream> #include<string> #include<queue> #include<algorithm> #include<functional> using namespace std; int main() { string s; while (cin >> s) { int n = s.length(); sort(s.begin(), s.end()); priority_queue<int, vector<int>, greater<int> > q; int i, j; for (i = 0; i < n; ) { for (j = i; j < n; ++j) { if (s[j] != s[i]){ q.push(j - i); i = j; break; } } if (j == n) { q.push(j - i); break; } } int res = 0; while (q.size() > 1) { int a = q.top(); q.pop(); int b = q.top(); q.pop(); res += a + b; q.push(a + b); } cout << res << endl; } return 0; }
C++ 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2020-09-06
#include<iostream> #include<string.h> #include<algorithm> #include<queue> using namespace std; int main(){ char str[1002]; while( cin >> str ){ int len=0,l = strlen(str),i,j; priority_queue< int,vector<int>,greater<int> > hufm;//定义优先队列,值小的在前。 sort(str,str+l);//排序后会把相同的字符放在一起便于后统计各个字符串个数。 for(i=0;i<l;){ j = i; while(str[j] == str[i] && j<l ) j++; hufm.push(j - i); i = j; } while( hufm.size() != 1 ){ int temp = 0; temp = hufm.top(); hufm.pop(); temp += hufm.top(); hufm.pop(); len += temp; hufm.push(temp); } cout << len << endl; } }
C++ 解法, 执行用时: 2ms, 内存消耗: 384KB, 提交时间: 2020-08-28
#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #include <utility> #include <queue> using namespace std; int main(){ char s[3300]; while(scanf("%s",s) != EOF){ int n = strlen(s); sort(s,s + n); priority_queue<int> heap; int cnt = 0; for(int i = 0,j;i < n;){ j = i; while(j < n && s[j] == s[i]) ++ j; heap.push(i - j); i = j; ++ cnt; } int ret = 0; for(int i = 0;i < cnt - 1;++ i){ int A = heap.top(); heap.pop(); int B = heap.top(); heap.pop(); ret -= A + B; heap.push(A + B); } printf("%d\n",ret); } return 0; }
C++ 解法, 执行用时: 2ms, 内存消耗: 392KB, 提交时间: 2021-04-24
#include <iostream> #include <algorithm> #include <queue> #include <cstring> using namespace std; const int maxn = 1e3; char s[maxn]; int main(){ while(scanf("%s", s) != EOF){ priority_queue<int, vector<int>, greater<int> > q;//优先级队列 小数优先级高 int len = strlen(s); sort(s, s + len); int l = 0, r = 1, cnt = 0; while(l < len){//计算各个字母重复的个数 放入优先级队列 小数优先级高 while(r < len && s[r] == s[l]) {r++;} q.push(r - l); l = r; cnt++; } int ans = 0; for(int i = 0; i < cnt - 1; i++){ //建树过程 实际上 带权路径长度和 = 所有建树过程中求的和全部加起来 int a = q.top(); q.pop(); int b = q.top(); q.pop(); ans += (a + b);//树的深度约多 被重复计算的次数也越多 q.push(a + b); } printf("%d\n", ans); } }
C++ 解法, 执行用时: 2ms, 内存消耗: 404KB, 提交时间: 2021-09-06
#include <iostream> #include <string> #include <algorithm> #include <vector> #include <queue> #include <unordered_map> using namespace std; int main(){ string str; priority_queue<int, vector<int>, greater<int>> que; unordered_map<char, int> umap; while(cin >> str){ for(char ch: str){ umap[ch]++; } for(auto& p: umap){ que.push(p.second); } int res = 0; while(que.size() != 1){ int temp = que.top(); que.pop(); temp += que.top(); que.pop(); res += temp; que.push(temp); } que.pop(); umap.clear(); cout << res<<endl; str = ""; } return 0; }