Even if the number of elements is more than two, it is possible with
bitwise operations, but it gets clumsy.

Suppose your alphabet has 4 characters. You can either:
- Count from 0 to (1<<4*n)-1 and use four bits to denote the selection
of the alphabet. Also, only one bit amongst those four should be set.
It is highly inefficient.
- Keep n nested loops and inside each loop you iterate from 0 to
(1<<4)-1 and use the standard bitwise operations. The con here is that
you have to hardcode the number of nested loops.

On Fri, Aug 5, 2011 at 2:44 PM, Nitin Nizhawan <[email protected]> wrote:
> @Varun  I think it can be done using bits, if input character set has only
> two elements. Or could u plz explain?
>
> On Fri, Aug 5, 2011 at 2:29 PM, Varun Jakhoria <[email protected]>
> wrote:
>>
>> I think it can be done using bitwise ANDing with a mask
>>
>> On Fri, Aug 5, 2011 at 12:58 PM, Gaurav Menghani
>> <[email protected]> wrote:
>> > An Implementation:
>> >
>> > #include<iostream>
>> > #include<string>
>> > using namespace std;
>> >
>> > string alphabet;
>> > int maxlen;
>> > void backtrack(string s,int l)
>> > {
>> >  if(l==maxlen) { cout<<s<<endl; return; }
>> >  s.push_back('-');
>> >  for(int i=0;i<alphabet.size();i++)
>> >        { s[l]=alphabet[i]; backtrack(s,l+1); }
>> > }
>> >
>> > int main()
>> > {
>> >  maxlen=3;
>> >  alphabet="op";
>> >  backtrack("",0);
>> >  return 0;
>> > }
>> >
>> >
>> > On Fri, Aug 5, 2011 at 12:42 PM, Kamakshii Aggarwal
>> > <[email protected]> wrote:
>> >> @gaurav:i could not understand ur sol.can u explain it again..
>> >>
>> >> On Fri, Aug 5, 2011 at 12:32 PM, Gaurav Menghani
>> >> <[email protected]>
>> >> wrote:
>> >>>
>> >>> On Fri, Aug 5, 2011 at 12:20 PM, Kamakshii Aggarwal
>> >>> <[email protected]> wrote:
>> >>> > given a set of letters and a length N, produce all possible
>> >>> > output.(Not
>> >>> > permutation). For example, give the letter (p,o) and length of 3,
>> >>> > produce
>> >>> > the following output(in any order you want, not just my example
>> >>> > order)
>> >>> >
>> >>> > ppp ppo poo pop opp opo oop ooo
>> >>> >
>> >>> > another example would be given (a,b) and length 2
>> >>> >
>> >>> > answer: ab aa bb ba
>> >>> >
>> >>> > --
>> >>> > Regards,
>> >>> > Kamakshi
>> >>> > [email protected]
>> >>>
>> >>> This can be done easily by backtracking
>> >>>
>> >>> void backtrack(string s, int l)
>> >>> {
>> >>>   if(l == maxlen) { cout<<s<<endl; return; }
>> >>>
>> >>>   s.push_back('-');
>> >>>   for(int i=0;i<alphabet.size();i++)
>> >>>   {
>> >>>     s[l]=alphabet[i];
>> >>>     backtrack(s,l+1);
>> >>>   }
>> >>> }
>> >>>
>> >>> --
>> >>> Gaurav Menghani
>> >>>
>> >>> --
>> >>> You received this message because you are subscribed to the Google
>> >>> Groups
>> >>> "Algorithm Geeks" group.
>> >>> To post to this group, send email to [email protected].
>> >>> To unsubscribe from this group, send email to
>> >>> [email protected].
>> >>> For more options, visit this group at
>> >>> http://groups.google.com/group/algogeeks?hl=en.
>> >>>
>> >>
>> >>
>> >>
>> >> --
>> >> Regards,
>> >> Kamakshi
>> >> [email protected]
>> >>
>> >> --
>> >> You received this message because you are subscribed to the Google
>> >> Groups
>> >> "Algorithm Geeks" group.
>> >> To post to this group, send email to [email protected].
>> >> To unsubscribe from this group, send email to
>> >> [email protected].
>> >> For more options, visit this group at
>> >> http://groups.google.com/group/algogeeks?hl=en.
>> >>
>> >
>> >
>> >
>> > --
>> > Gaurav Menghani
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups "Algorithm Geeks" group.
>> > To post to this group, send email to [email protected].
>> > To unsubscribe from this group, send email to
>> > [email protected].
>> > For more options, visit this group at
>> > http://groups.google.com/group/algogeeks?hl=en.
>> >
>> >
>>
>>
>>
>> --
>> Varun Jakhoria
>> ...it's only about 0's & 1's
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Gaurav Menghani

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to