Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- char st [20];
- int n;
- int check (int k)
- {
- if (abs (st [k] - st [k - 1]) > 1)
- return 0;
- if (st [k] == st [k - 1])
- return 0;
- if (k == n - 2)
- if (abs (st [k] - st [n - 1]) > 1)
- return 0;
- return 1;
- }
- void back (int k)
- {
- for (char i = 'a'; i <= 'z'; i ++)
- {
- st [k] = i;
- if (check (k))
- {
- if (k == n - 2)
- cout << st << "\n";
- else
- back (k + 1);
- }
- }
- }
- int main()
- {
- cin >> n;
- st [0] = 'a';
- st [n - 1] = 'a';
- back (1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment