v 0. Pasted by slipstak2 as cpp at 2011-02-08 13:09:04 MSK and set expiration to never.

Paste will expire never.

  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <cstdio>
  5.  
  6. using namespace std;
  7.  
  8. int n;
  9. string a;
  10. bool next_permutation(string &a) {
  11.  
  12.     int j = n-2;
  13.     while (j!=-1 && a[j] > a[j+1]) j--;
  14.     if (j == -1)
  15.         return false; // a - last permutation
  16.     int k = n - 1;
  17.     while (a[j] > a[k]) k--;
  18.  
  19.     swap(a[j],a[k]);
  20.  
  21.     // reverse back [j+1, n-1]
  22.     int l = j + 1, r = n - 1;
  23.     while (l<r)
  24.         swap(a[l++],a[r--]);
  25.     return true;
  26. }
  27. void L()
  28. {
  29.     do
  30.     {
  31.         for (int i=0;i<n;i++)
  32.             cout<<a[i];
  33.         cout<<endl;
  34.     }
  35.     while (next_permutation(a));
  36. }
  37. void solve() {
  38.  
  39.     sort(a.begin(), a.end());
  40.     L();
  41. }
  42. void input() {
  43.  
  44.     cin>>a;
  45.     n = a.size();
  46. }
  47. int main() {
  48.     freopen("input.txt","r",stdin);
  49.     freopen("output.txt","w",stdout);
  50.    
  51.     input();
  52.     solve();
  53.    
  54.     return 0;
  55. }


Editing is locked.