Paste will expire never.
- #include <iostream>
- #include <vector>
- #include <cstdio>
- #include <string>
- using namespace std;
- bool next_permutation_swap(vector<int> &p, // перестановка
- vector<int> &y, // таблица инверсий
- vector<int> &d) // направление движений
- {
- int n = p.size();
- int i;
- int next = 0;
- for (i=n-1;i>=0;i--) {
- int border = d[i] == 1 ? i : 0;
- if (y[i] != border) {
- y[i] += d[i];
- next = d[i];
- break;
- }
- else
- d[i] = -d[i];
- }
- if (!next) // последняя перестановка
- return false;
- for (int j = 0; j < n; j++)
- if (p[j] == i) {
- swap(p[j],p[j+d[i]]);
- break;
- }
- return true;
- }
- int main()
- {
- freopen("input.txt","r",stdin);
- freopen("output.txt","w",stdout);
- string a;
- cin>>a; // можно не сортить
- int n = a.size();
- vector<int> p(n); // перестановка
- vector<int> y(n); // таблица инверсий
- vector<int> d(n,1); // направление движений
- for (int i=0;i<n;i++)
- p[i] = n-1-i;
- do
- {
- for (int i=0;i<n;i++)
- cout<<a[p[i]];
- cout<<endl;
- } while (next_permutation_swap(p,y,d));
- return 0;
- }
Editing is locked.