关于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;
}
安克创新 Anker公司福利 814人发布
查看22道真题和解析