Mikhail-Podbolotov

Untitled

May 29th, 2024
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. bool contain(string& str, char c) {
  6.     for (char ch : str) {
  7.         if (ch == c) {
  8.             return true;
  9.         }
  10.     }
  11.     return false;
  12. }
  13. string numbers(int n) {
  14.     string res = "";
  15.     n = abs(n);
  16.     if (n == 0) return "0";
  17.     while (n > 0) {
  18.         char digit = '0' + (n % 10);
  19.         if (!contain(res, digit)) {
  20.             res+=digit;
  21.         }
  22.         n /= 10;
  23.     }
  24.     int length = res.length();
  25.     for (int i = 0; i < length - 1 ; ++i) {
  26.         for (int j = i + 1; j < length; ++j) {
  27.             if (res[j] < res[i]) {
  28.                 swap(res[j], res[i]);
  29.             }
  30.         }
  31.     }
  32.     return res;
  33.  
  34. }
  35. void Print(int n, int* Array, int mask) {
  36.     for (int i = 0; i < n; ++i) {
  37.         if (mask & (1 << i)) {
  38.             cout << Array[i] << " ";
  39.         }
  40.     }
  41.     cout << endl;
  42. }
  43. bool F(int n, int* Array, int mask) {
  44.     string cur ="";
  45.     for (int i = 0; i < n; ++i) {
  46.         if (mask & (1 << i)) {
  47.             if (cur == "") cur = numbers(Array[i]);
  48.             else {
  49.                 if (cur != numbers(Array[i])) return false;
  50.             }
  51.         }
  52.     }
  53.     return true;
  54. }
  55. int main()
  56. {
  57.     ifstream file("input.txt");
  58.     int n = 0;
  59.     file >> n;
  60.     int* Array = new int[n];
  61.     for (int i = 0; i < n; ++i) {
  62.         file >> Array[i];
  63.     }
  64.     int k;
  65.     cin >> k;
  66.     unsigned int mask = (1<<k)-1;
  67.     while (mask < (1 << n)) {
  68.         if (F(n, Array, mask)) Print(n, Array, mask);
  69.         int i = 0, count = 0;
  70.         for (; !(mask & (1 << i)); i++);
  71.         for (; mask & (1 << i); i++) { count++; mask &= ~(1 << i); }
  72.         mask |= (1 << i++);
  73.         count--;
  74.         mask |= (1 << count) - 1;
  75.     }
  76. }
Add Comment
Please, Sign In to add comment