Just to increase the size of the string by one. Then you can put any character at the the new last position, which is 'l'.
On Fri, Aug 5, 2011 at 2:34 PM, Kamakshii Aggarwal <[email protected]> wrote: > @gaurav:can u please explain what is the purpose of this line.. > s.push_back('-'); > > On Fri, Aug 5, 2011 at 1:10 PM, Nitin Nizhawan <[email protected]> > wrote: >> >> Or one could just simulate a counting from 0 to (numchars^N)-1 in base >> numchars. >> ....... >> code: >> void printit(int N,char chars[],int index[]){ >> for(int i=0;i<N;i++){ >> printf("%c",chars[index[i]]); >> } >> printf("\n"); >> } >> void generate(int numchars,char chars[],int N){ >> int index[100]={0}; >> int allmax=0; >> int maxdigit=numchars-1; >> printit(N,chars,index); >> while(!allmax){ >> // add one; >> index[0]++; >> allmax=0; >> for(int i=0;i<N;i++){ >> if(index[i]>=numchars){ >> index[i]=0; index[i+1]++; >> } >> if(i==0&&index[i]==maxdigit){ >> allmax=1; >> } >> >> allmax = (index[i]==maxdigit)?allmax&1:0; >> >> } >> printit(N,chars,index); >> } >> } >> int main(){ >> char chars [] ={'p','o'}; >> int numchars =sizeof(chars)/sizeof(char); >> int N=3; >> generate(numchars,chars,N); >> } >> 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. >>> >> >> -- >> 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.
