Advertisement
STANAANDREY

pb19 9/11/2020

Nov 9th, 2020
1,784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include <iostream>
  2. #include<cstring>
  3. using namespace std;
  4. #define NMAX 12
  5. int st[NMAX], n, pos[NMAX];
  6. char s[NMAX][30];
  7.  
  8. void read() {
  9.     cin >> n;
  10.     for (int i = 1; i <= n; i++) {
  11.         cin >> s[i];
  12.     }
  13. }
  14.  
  15. void tipar(int k) {
  16.     for (int i = 1; i <= k; i++)
  17.         cout << s[st[i]] << ' ';
  18.     cout << endl;
  19. }
  20.  
  21. int valid(int k) {
  22.     char v[30] = "", v2[30] = "";
  23.     if (k > 1) {
  24.         for (int i = 1; i <= n; i++) {
  25.             if (!strcmp(s[i], s[st[k]])) {
  26.                 strcpy(v, s[i - 1]);
  27.                 strcpy(v2, s[i + 1]);
  28.                 break;
  29.             }
  30.         }
  31.         if (!strcmp(s[st[k - 1]], v) || !strcmp(s[st[k - 1]], v2))
  32.             return 0;
  33.     }//*/
  34.     for (int i = 1; i < k; i++)
  35.         if (st[i] == st[k])
  36.             return 0;
  37.     return 1;
  38. }
  39.  
  40. int sol(int k) {
  41.     return k == n;
  42. }
  43.  
  44. void bktr() {
  45.     int k = 1;
  46.     st[k] = 0;
  47.     while (k) {
  48.         if (st[k] < n) {
  49.             st[k]++;
  50.             if (valid(k)) {
  51.                 if (sol(k))
  52.                     tipar(k);
  53.                 else {
  54.                     k++;
  55.                     st[k] = 0;
  56.                 }
  57.             }
  58.         }
  59.         else
  60.             k--;
  61.     }
  62. }
  63.  
  64. int main() {
  65.     read();
  66.     bktr();
  67.     return 0;
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement