STANAANDREY

tema prod cart1

Nov 3rd, 2020 (edited)
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. int n = 5, card[20], st[20], nrsol;
  5. char alfa[7][30], v[] = "AEIOU";
  6.  
  7. void init() {
  8.     for (int i = 0; i < int('Z' - 'A' + 1); i++)
  9.         alfa[1][i] = char(i + 'A');
  10.     card[1] = strlen(alfa[1]);
  11.     for (int i = 2; i <= n; i++) {
  12.         strcpy(alfa[i], alfa[1]);
  13.         card[i] = card[1];
  14.     }
  15. }
  16.  
  17. int valid(int k) {
  18.     if (k > 1 && strchr(v, alfa[k][st[k] - 1]) && strchr(v, alfa[k - 1][st[k - 1] - 1]))
  19.             return 0;
  20.     return 1;
  21. }
  22.  
  23. int sol(int k) {
  24.     return k == n;
  25. }
  26.  
  27. void tipar(int k) {
  28.     nrsol++;
  29.     for (int i = 1; i <= k; i++)
  30.         cout << alfa[i][st[i] - 1];
  31.     cout << '\n';
  32. }
  33.  
  34. void bktr() {
  35.     int k = 1;
  36.     st[k] = 0;
  37.     while (k > 0) {
  38.         if (st[k] < card[k]) {
  39.             st[k]++;
  40.             if (valid(k)) {
  41.                 if (sol(k))
  42.                     tipar(k);
  43.                 else {
  44.                     k++;
  45.                     st[k] = 0;
  46.                 }
  47.             }
  48.         }
  49.         else
  50.             k--;
  51.     }
  52. }
  53.  
  54. int main() {
  55.     init();
  56.     bktr();
  57.     cout << nrsol;
  58.     return 0;
  59. }
  60.  
Add Comment
Please, Sign In to add comment