关于b题

为什么我排序后,按最大字符来循环len来输出wa了呢,我看很多人都用优先队列了,这是什么原因呢

#include<bits/stdc++.h>
#define int long long
using namespace std;

typedef pair<int, char> PII;
const int N = 30;
PII a[N], b[N];

bool cmp (PII x, PII y) {
    return x.first > y.first;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    string s; cin >> s;
    int n = s.size();
    s = '?' + s;
    map<char, int> mp;
    int idx = 0;
    for (int i = 1; i <= n; i++) mp[s[i]]++;
    for (char i = 'a'; i <= 'z'; i++) {
        if (mp[i] == 0) continue;
        a[++idx] = {mp[i], i};
    }
    sort (a + 1, a + 1 + idx, cmp);
    int len = min((int)26, n);
    vector<char> v;
    while (len) {
        for (int i = 1; i <= idx; i++) b[i] = a[i];
        int f = 0;
        int cnt = 0;
        v.clear();
        for (int i = 1; i <= idx; i++) {
            if (b[i].first != 0) {
                b[i].first--;
                v.push_back(b[i].second);
                cnt++;
            }
            if (cnt == len) {
                cnt = 0;
                i = 0;
            }
        }
        for (int i = 1; i <= idx; i++) {
            if (b[i].first != 0) f = 1;
        }
        if (f == 0) break;
        len--;
    }
    cout << len << "\n";
    for (auto it: v) cout << it;
    return 0;
}

全部评论
稍微看了一下,输入aaaabbbbcdef,你没考虑ab数量时应该按ascll排
点赞 回复 分享
发布于 03-23 16:52 台湾

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务