fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <map>
  4. #include <set>
  5. #include <cmath>
  6. #include <vector>
  7. #include <climits>
  8. #include <string>
  9. #include <algorithm>
  10. #include <sstream>
  11. using namespace std;
  12.  
  13. vector<string> split(string haystack, string needle) {
  14. vector<string> result;
  15. int startPos = 0;
  16. int foundPos = haystack.find(needle, startPos);
  17. while (foundPos != string::npos) {
  18. int count = foundPos - startPos;
  19. string token = haystack.substr(startPos, count);
  20. if (!token.empty()) {
  21. result.push_back(token);
  22. }
  23. startPos = foundPos + needle.length();
  24. foundPos = haystack.find(needle, startPos);
  25. }
  26. string token = haystack.substr(startPos, haystack.length() - startPos);
  27. if (!token.empty()) {
  28. result.push_back(token);
  29. }
  30. return result;
  31. }
  32.  
  33. int main() {
  34. ios_base::sync_with_stdio(false);
  35. cin.tie(NULL);
  36. string s;getline(cin, s);
  37.  
  38. vector<string> parts = split(s, " ");
  39.  
  40. sort(parts.begin(), parts.end());
  41.  
  42. for (auto value : parts) {
  43. cout << value << " ";
  44. }
  45.  
  46. cout << endl;
  47.  
  48. sort(parts.begin(), parts.end(), [](const string& a, const string& b) {
  49. if (a.length() != b.length()) {
  50. return a.length() < b.length();
  51. }
  52. return a < b;
  53. });
  54.  
  55. for (auto value : parts) {
  56. cout << value << " ";
  57. }
  58. return 0;
  59. }
  60.  
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout