Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int st[27], n, p;
- void tipar(int k)
- {
- for (int i = 1; i <= k; i++)
- cout << (char)(st[i] + 64);
- cout << endl;
- }
- int solutie(int k)
- {
- return k == p;
- }
- int validare(int k)
- {
- if (k > 1 && st[k] < st[k - 1])
- return 0;
- for (int i = 1; i < k; i++)
- if (st[i] == st[k])
- return 0;
- return 1;
- }
- void bktr()
- {
- int k = 1;
- st[k] = 0;
- while (k)
- {
- if (st[k] < n)
- {
- st[k]++;
- if (validare(k))
- {
- if (solutie(k))
- tipar(k);
- else
- {
- k++;
- st[k] = 0;
- }
- }
- }
- else
- k--;
- }
- }
- int main ()
- {
- cin >> n >> p;
- bktr();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement