Advertisement
STANAANDREY

gen cuv 3/11/2020

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