Wooph

LitereGen1

Mar 10th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. char st [20];
  6. int n;
  7.  
  8. int check (int k)
  9. {
  10.     if (abs (st [k] - st [k - 1]) > 1)
  11.         return 0;
  12.  
  13.     if (st [k] == st [k - 1])
  14.         return 0;
  15.  
  16.     if (k == n - 2)
  17.         if (abs (st [k] - st [n - 1]) > 1)
  18.             return 0;
  19.  
  20.     return 1;
  21. }
  22.  
  23. void back (int k)
  24. {
  25.     for (char i = 'a'; i <= 'z'; i ++)
  26.     {
  27.         st [k] = i;
  28.         if (check (k))
  29.         {
  30.             if (k == n - 2)
  31.                 cout << st << "\n";
  32.             else
  33.                 back (k + 1);
  34.         }
  35.     }
  36. }
  37.  
  38. int main()
  39. {
  40.     cin >> n;
  41.     st [0] = 'a';
  42.     st [n - 1] = 'a';
  43.     back (1);
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment