Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- /**
- 1. Stiva de lungime n
- 2. Pe stiva punem litere consecutive ale alfabetului
- 3. Validare: v[1]=v[n]='a'
- */
- char lit[]="abcdefghijklmopqrstuvwxyz";
- int n,st[27];
- void Afisare()
- {
- for(int i=1;i<=n;i++)
- cout<<lit[st[i]];
- cout<<"\n";
- }
- int Modul(int x)
- {
- if(x<0) x=-x;
- return x;
- }
- int Valid(int top)
- {
- if(top==n-1 and st[top]!= 1) return 0;
- if(top==n and st[top]!=0) return 0;
- if(top>1 and Modul(st[top]-st[top-1])!=1) return 0;
- return 1;
- }
- void Back()
- {
- int cand,top;
- top=1;
- st[top]=0;
- top=2;
- while(top>1)
- {
- cand=0;
- while(!cand and st[top]<25)
- {
- st[top]++;
- cand=Valid(top);
- }
- if(!cand) top--;
- else if(top==n) Afisare();
- else st[++top]=-1;
- }
- }
- int main()
- {
- cin>>n;
- Back();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement