#define _CRT_SECURE_NO_WARNINGS #include <stdbool.h> #include <stdio.h> #include <string.h> #define MAX_LENGTH 12 bool permutation(const char* str, int size); bool next(char* str, int size); int main(void) { char P[MAX_LENGTH + 1]; int N; if (fgets(P, MAX_LENGTH + 1, stdin) == NULL) { printf("bad input\n"); return 1; } if (P[strcspn(P, "\n")] == '\n') { P[strcspn(P, "\n")] = '\0'; } if (scanf("%d", &N) != 1) { printf("bad input\n"); return 1; } int size = (int)strlen(P); if (!permutation(P, size)) { printf("bad input"); return 0; } int d = 0; while (d < N && next(P, size)) { printf("%s\n", P); ++d; } return 0; } bool permutation(const char* str, int size) { bool s[10] = { false }; for (int i = 0; i < size; i++) { if (!isdigit(str[i])) return false; if (s[str[i] - '0']) return false; s[str[i] - '0'] = true; } return true; } bool next(char* str) { int n = strlen(str); int i = n - 2; while (i >= 0 && str[i] >= str[i + 1]) { i--; } if (i < 0) return false; int j = n - 1; while (str[j] <= str[i]) { j--; } char temp = str[i]; str[i] = str[j]; str[j] = temp; for (int k = i + 1, l = n - 1; k < l; k++, l--) { temp = str[k]; str[k] = str[l]; str[l] = temp; } return true; }