Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<cstring>
- using namespace std;
- #define NMAX 12
- int st[NMAX], n, pos[NMAX];
- char s[NMAX][30];
- void read() {
- cin >> n;
- for (int i = 1; i <= n; i++) {
- cin >> s[i];
- }
- }
- void tipar(int k) {
- for (int i = 1; i <= k; i++)
- cout << s[st[i]] << ' ';
- cout << endl;
- }
- int valid(int k) {
- char v[30] = "", v2[30] = "";
- if (k > 1) {
- for (int i = 1; i <= n; i++) {
- if (!strcmp(s[i], s[st[k]])) {
- strcpy(v, s[i - 1]);
- strcpy(v2, s[i + 1]);
- break;
- }
- }
- if (!strcmp(s[st[k - 1]], v) || !strcmp(s[st[k - 1]], v2))
- return 0;
- }//*/
- for (int i = 1; i < k; i++)
- if (st[i] == st[k])
- return 0;
- return 1;
- }
- int sol(int k) {
- return k == n;
- }
- void bktr() {
- int k = 1;
- st[k] = 0;
- while (k) {
- if (st[k] < n) {
- st[k]++;
- if (valid(k)) {
- if (sol(k))
- tipar(k);
- else {
- k++;
- st[k] = 0;
- }
- }
- }
- else
- k--;
- }
- }
- int main() {
- read();
- bktr();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement