1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| #include <iostream> #include <cstdio> #include <queue> #include <vector> #include <string> #include <cstring> using namespace std;
namespace SHAWN { const int N = 1e6 + 7; struct node { int ma,val,fa,l,r,pos; }; struct cmp{ bool operator()(const node&a, const node&b)const{ if(a.val != b.val) { return a.val > b.val; } else { return a.ma > b.ma; } } }; priority_queue<node,vector<node>,cmp> q; string s; node tree[N]; vector<int> haff[30]; int used[30]; int n, m, idx; void build() { while (q.size() >= 2) { node x = q.top(); q.pop(); node y = q.top(); q.pop(); if (!x.pos) { tree[++idx] = x; tree[idx].pos = x.pos = idx; } if (!y.pos) { tree[++idx] = y; tree[idx].pos = y.pos = idx; } node z = {-1,(x.val+y.val), -1, x.pos, y.pos, ++idx}; tree[x.pos].fa = tree[y.pos].fa = idx; q.push(z); tree[idx] = z; } } void solve() { for (int i = 1; i <= idx; ++i) { if (tree[i].ma != -1) { int now = i; while (tree[now].fa != -1) { if (now == tree[tree[now].fa].l) { haff[tree[i].ma].push_back(0); } else { haff[tree[i].ma].push_back(1); } now = tree[now].fa; } } } for (int i = 1; i <= 27; ++i) { if (haff[i].size()) { for(int j = haff[i].size()-1; j >= 0; --j) { cout << haff[i][j]; } cout << '\n'; } } for (int i = 0; i <= n; ++i) { int a = s[i] - 'a' + 1; for(int j = haff[a].size()-1; j >= 0; --j) { cout << haff[a][j]; } } } int work() { cin >> n >> m >> s; for (int i = 0; i < n; ++i) { used[s[i] - 'a' + 1]++; } for (int i = 1; i <=26; ++i) { if (used[i]) { q.push({i,used[i],0,-1,-1,0}); } } build(); solve(); return 0; } }
signed int main() { ios :: sync_with_stdio(false); cin.tie(nullptr); return SHAWN :: work(); }
|