思路参照这个帖子,重新写的代码
typedef pair<int, int> PII;
class Solution {
public:
void addStr(string &str, vector<PII> &arr, int i, int maxv) {
int used = min(maxv, arr[i].first);
arr[i].first -= used;
while (used--)
str += 'a' + arr[i].second;
}
string longestDiverseString(int a, int b, int c) {
vector<PII> arr = { {a, 0}, {b, 1}, {c, 2} };
string ans ="";
while (1) {
sort(arr.begin(), arr.end());
if (ans.size() && ans.back() == 'a' + arr[2].second) {
if (arr[1].first == 0)
return ans;
addStr(ans, arr, 1, 1);
}
else {
if (arr[2].first == 0)
return ans;
addStr(ans, arr, 2, 2);
}
}
return "";
}
};