直接sort:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>using namespace std;int main()
{string str;cin >> str;int len = str.size();vector<string> strings;for(int i = 0; i < len; i++){strings.push_back(str.substr(i));}sort(strings.begin(), strings.end());for (int i = 0; i < strings.size(); i++) {cout << strings[i] << endl;}return 0;
}
冒泡排序:
#include <iostream>
#include <vector>using namespace std;int main()
{string str;cin >> str;int len = str.size();vector<string> strings;for(int i = 0; i < len; i++){strings.push_back(str.substr(i));}// 冒泡排序for (int i = 0; i < strings.size(); i++) {for (int j = 0; j < strings.size() - i - 1; j++) {if (strings[j] > strings[j + 1]) {swap(strings[j], strings[j + 1]);}}}for (int i = 0; i < strings.size(); i++) {cout << strings[i] << endl;}return 0;
}