Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <string>
- using namespace std;
- bool contain(string& str, char c) {
- for (char ch : str) {
- if (ch == c) {
- return true;
- }
- }
- return false;
- }
- string numbers(int n) {
- string res = "";
- n = abs(n);
- if (n == 0) return "0";
- while (n > 0) {
- char digit = '0' + (n % 10);
- if (!contain(res, digit)) {
- res+=digit;
- }
- n /= 10;
- }
- int length = res.length();
- for (int i = 0; i < length - 1 ; ++i) {
- for (int j = i + 1; j < length; ++j) {
- if (res[j] < res[i]) {
- swap(res[j], res[i]);
- }
- }
- }
- return res;
- }
- void Print(int n, int* Array, int mask) {
- for (int i = 0; i < n; ++i) {
- if (mask & (1 << i)) {
- cout << Array[i] << " ";
- }
- }
- cout << endl;
- }
- bool F(int n, int* Array, int mask) {
- string cur ="";
- for (int i = 0; i < n; ++i) {
- if (mask & (1 << i)) {
- if (cur == "") cur = numbers(Array[i]);
- else {
- if (cur != numbers(Array[i])) return false;
- }
- }
- }
- return true;
- }
- int main()
- {
- ifstream file("input.txt");
- int n = 0;
- file >> n;
- int* Array = new int[n];
- for (int i = 0; i < n; ++i) {
- file >> Array[i];
- }
- int k;
- cin >> k;
- unsigned int mask = (1<<k)-1;
- while (mask < (1 << n)) {
- if (F(n, Array, mask)) Print(n, Array, mask);
- int i = 0, count = 0;
- for (; !(mask & (1 << i)); i++);
- for (; mask & (1 << i); i++) { count++; mask &= ~(1 << i); }
- mask |= (1 << i++);
- count--;
- mask |= (1 << count) - 1;
- }
- }
Add Comment
Please, Sign In to add comment