Iamtui1010

qbhv

Nov 7th, 2021
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. //#include<bits/stdc++.h>
  2. #include<iostream>
  3. #include<fstream>
  4. #include<vector>
  5. #include<algorithm>
  6. #include<set>
  7.  
  8. #define long long long
  9. #define nln '\n'
  10.  
  11. const long N = 20;
  12.  
  13. using namespace std;
  14.  
  15. // GLobal variables: cow, m, n, a
  16.  
  17. fstream f1,f2;
  18.  
  19. inline void openf()
  20. {
  21.     f1.open("qbhv.inp", ios:: in);
  22.     f2.open("qbhv.out", ios:: out);
  23. }
  24.  
  25. inline void closef()
  26. {
  27.     f1.close();
  28.     f2.close();
  29. }
  30.  
  31. string str;
  32. long n;
  33. vector<short> a;
  34.  
  35. void data()
  36. {
  37.     f1.tie(0)->sync_with_stdio(0);
  38.     f2.tie(0)->sync_with_stdio(0);
  39.     //cin.tie(0)->sync_with_stdio(0);
  40.     cin >> str;
  41.     n = str.size();
  42.  
  43.     for (long i = 0; i != n; ++i)
  44.         a.push_back(i);
  45. }
  46.  
  47. set<string> sav;
  48.  
  49. void show()
  50. {
  51.     string tem = "";
  52.     for (const auto &i : a)
  53.         tem += str[i];
  54.     sav.insert(tem);
  55. }
  56.  
  57. void swap(long &a, long &b)
  58. {
  59.     long itm = a;
  60.     a = b;
  61.     b = itm;
  62. }
  63.  
  64. bool stop = 0;
  65.  
  66. void gen()
  67. {
  68.     long i = n-2;
  69.     while (a[i] > a[i+1] && i != -1)
  70.         --i;
  71.     if (i == -1)
  72.     {
  73.         stop = 1;
  74.         return;
  75.     }
  76.  
  77.     long j = n-1;
  78.     while (a[j] < a[i])
  79.         --j;
  80.  
  81.     swap(a[i], a[j]);
  82.  
  83.     long lef = i+1, rig = n-1;
  84.     while (lef < rig)
  85.     {
  86.         swap(a[lef], a[rig]);
  87.         ++lef; --rig;
  88.     }
  89. }
  90.  
  91. void process()
  92. {
  93.     while (!stop)
  94.     {
  95.         show();
  96.         gen();
  97.     }
  98. }
  99.  
  100. void view()
  101. {
  102.     cout << sav.size() << nln;
  103.     for (const auto &i : sav)
  104.         cout << i << nln;
  105. }
  106.  
  107. int main()
  108. {
  109.     openf();
  110.     data();
  111.     process();
  112.     view();
  113.     closef();
  114.     return 0;
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment