fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <stack>
  4. using namespace std;
  5.  
  6.  
  7. class Comp{
  8. public:
  9. bool operator()(const string &a, const string &b) const{
  10. for(int i = 0; i < a.length() && i < b.length(); ++i){
  11. if(a[i] < b[i]) return true;
  12. else if(a[i] > b[i]) return false;
  13. }
  14. return a.length() < b.length();
  15. }
  16. };
  17.  
  18. template<typename T, typename C>
  19. void sort(vector<T> &v, C comp){
  20. typedef pair<int, int> P;
  21. stack<P> st;
  22. st.push(P(0, v.size()));
  23. while(!st.empty()){
  24. P s = st.top(); st.pop();
  25. if(s.first + 1 == s.second) continue;
  26. string piv = v[(s.first + s.second) / 2];
  27. int i = s.first, j = s.second - 1;
  28. while(true){
  29. while(i < s.second && comp(v[i], piv)) ++i;
  30. while(j >= s.first && comp(piv, v[j])) --j;
  31. if(i >= j) break;
  32. swap(v[i], v[j]);
  33. ++i; --j;
  34. }
  35. st.push(P(s.first, i));
  36. st.push(P(i, s.second));
  37. }
  38. }
  39.  
  40. vector<string> cycle(string s){
  41. vector<string> res;
  42. for(int i = 0; i < s.length(); ++i){
  43. res.push_back(s);
  44. s = s.substr(1) + s[0];
  45. }
  46. return res;
  47. }
  48.  
  49. int main(void){
  50. string str;// = "apple";
  51. cin >> str;
  52. vector<string> a = cycle(str);
  53. for(int i = 0; i < a.size(); ++i) cout << a[i] << endl;
  54. cout << endl;
  55. sort(a, Comp());
  56. for(int i = 0; i < a.size(); ++i) cout << a[i] << endl;
  57. cout << endl;
  58. return 0;
  59. }
  60.  
Success #stdin #stdout 0.01s 2868KB
stdin
apple
stdout
apple
pplea
pleap
leapp
eappl

apple
eappl
leapp
pleap
pplea