fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. void getRes(string& res, vector<pair<int, char>>& v, int i){
  8. if(v[i].second == res.back() || v[i].first == 0) return;
  9. if(v[i].first > (v[(i+1)%3].first + v[(i+2)%3].first)){
  10. res.push_back(v[i].second);
  11. v[i].first--;
  12. if(v[i].first > 0) {
  13. res.push_back(v[i].second);
  14. v[i].first--;
  15. }
  16. } else if (v[i].first < (v[(i+1)%3].first-v[(i+2)%3].first) && v[(i+2)%3].first > 0){
  17. res.push_back(v[(i+2)%3].second);
  18. v[(i+2)%3].first--;
  19. } else if(v[i].first < (v[(i+2)%3].first - v[(i+1)%3].first) && v[(i+1)%3].first > 0){
  20. res.push_back(v[(i+1)%3].second);
  21. v[(i+1)%3].first--;
  22. } else{
  23. res.push_back(v[i].second);
  24. v[i].first--;
  25. }
  26. }
  27. string longestDiverseString(int a, int b, int c) {
  28. vector<pair<int, char>> v = {{a, 'a'}, {b, 'b'}, {c, 'c'}};
  29. sort(v.begin(), v.end());
  30. string res = "";
  31. bool flag = true;
  32. while(flag){
  33. for(int i=2; i>=0; i--){
  34. getRes(res, v, i);
  35. if(v[(i+1)%3].first == 0 && v[(i+2)%3].first == 0){
  36. flag = false;
  37. }
  38. }
  39. }
  40. cout<<"长度="<<res.length()<<endl;
  41. return res;
  42. }
  43.  
  44. int main() {
  45. // your code goes here
  46. cout<<longestDiverseString(5,50,10)<<endl;
  47. return 0;
  48. }
Success #stdin #stdout 0s 4952KB
stdin
Standard input is empty
stdout
长度=47
bbabbabbabbabbabbcbbcbbcbbcbbcbbcbbcbbcbbcbbcbb