combination of strings

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 52
Reputation: shankhs is an unknown quantity at this point 
Solved Threads: 1
shankhs shankhs is offline Offline
Junior Poster in Training

combination of strings

 
0
  #1
May 18th, 2008
I have a problem in printing out the permutation of various characters grouped together like
string 0 has"ABC"
1: "DEF"
2:"GHI"
3:"JKL"
4."MNO"
5."PRS"
6."TUV"
7."WXY"

now i have to print all the permutation of the characters to form 1 letter to 12 letter words
lets say i have 2512
then my possible permutation will be 81 strings like GPDG GPDH GPDI GPEG GPEH GPEI GPFG GPFH GPFI GRDG GRDH GRDI ........

and the number can be even 25123 or longer
so one of the possible approach is recursion:
so I tried:
  1. char rec(string in[],vector<int> brand,int i,int j,int l)
  2. {
  3. if(j==l){
  4. j=0;
  5. name+=rec(in,brand,++i,++j,l);
  6. --l;
  7. return in[brand[i]][j];
  8. }
  9. if(l==-1)
  10. {
  11. l=2;j=0;i+=1;
  12. }
  13. name+=rec(in,brand,i,++j,l);
  14. }
PLZ help me as I am learning recursion by myself and its a very hard topic.......
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 6
Reputation: bulg is an unknown quantity at this point 
Solved Threads: 1
bulg bulg is offline Offline
Newbie Poster

Re: combination of strings

 
0
  #2
May 18th, 2008
The best way to start to learn recursion is to check out a Binary tree, and try some of the sorting routines associated w/ them... that should be very easy to understand.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: combination of strings

 
0
  #3
May 18th, 2008
I don't imagine that recursion and permutations are very safe together without tail recursion.

Use a modulo counter. For example, in the Arabic number system we use we have ten digits for each power.
0 1 2 3 4 5 6 7 8 9

To count through them all, just start counting.
  1. // Print 0123456789... ad infinitum
  2. for (unsigned u = 0; ; u++)
  3. cout << (u % 10);

Take it one step further, and the thing you print doesn't actually have to be the digit, it can be anything you can index in a ten-element array.
  1. // Print abcdefghij... ad infinitum
  2. char letters[] = "abcdefghij";
  3. for (unsigned u = 0; ; u++)
  4. cout << letters[ u % 10 ];

The modulus doesn't need to be ten. It can be anything you want, like three.

That should be enough to get you started.
Last edited by Duoas; May 18th, 2008 at 8:05 pm. Reason: I never learned to count to ten
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 30
Reputation: Compton11 is an unknown quantity at this point 
Solved Threads: 0
Compton11 Compton11 is offline Offline
Light Poster

Re: combination of strings

 
0
  #4
May 19th, 2008
shanksh, why don't you use the next_permutation() function in the <algorithm> library? I'm not sure if you are allowed to use it for your assignment, but if you can, here's a very helpful link on how to use it:

http://www.cplusplus.com/reference/a...rmutation.html

Hope this helps...
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 52
Reputation: shankhs is an unknown quantity at this point 
Solved Threads: 1
shankhs shankhs is offline Offline
Junior Poster in Training

Re: combination of strings

 
0
  #5
May 19th, 2008
hi compton11 thanx for the link look i have 2 reasons not to use next_permutation:
1.I am learnung recursion (dat btw i am doing it by myself no instructor)
2.next_permutation gives permutation of characters in a set not from different sets....
So only technique left is RECURSION.
Thanx for ur help
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 52
Reputation: shankhs is an unknown quantity at this point 
Solved Threads: 1
shankhs shankhs is offline Offline
Junior Poster in Training

Re: combination of strings

 
0
  #6
May 19th, 2008
hi compton11 thanx for the link look i have 2 reasons not to use next_permutation:
1.I am learnung recursion (dat btw i am doing it by myself no instructor)
2.next_permutation gives permutation of characters in a set not from different sets....
So only technique left is RECURSION.
Ok if u havent understood the question the problem is to permute the characters taking from different sets.....
LIKE if you have to take sets 2512:
then there will be 81 strings of length 4 and characters for every string is taken in order {"char from2","char from 5","char from1","char from2",}(this is one string).....
So i think i am now clear?????
Thanx for ur help
Last edited by shankhs; May 19th, 2008 at 11:28 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: combination of strings

 
0
  #7
May 20th, 2008
Originally Posted by shankhs View Post
hi compton11 thanx for the link look i have 2 reasons not to use next_permutation:
1.I am learnung recursion (dat btw i am doing it by myself no instructor)
2.next_permutation gives permutation of characters in a set not from different sets....
So only technique left is RECURSION.
Ok if u havent understood the question the problem is to permute the characters taking from different sets.....
LIKE if you have to take sets 2512:
then there will be 81 strings of length 4 and characters for every string is taken in order {"char from2","char from 5","char from1","char from2",}(this is one string).....
So i think i am now clear?????
Thanx for ur help
I understand what you are doing, but I don't understand the algorithm you are trying to implement using that recursive function. For me, at least, I'd need some comments as to what each variable represents and I'd like to see the call from the main function in order to get my hands around what you are trying to do. Somewhere you need a vector of strings (or some other form of storage of multiple strings) unless you are displaying them immediately and not storing them. At some point you end up with 81 (in this case) strings. Where are those strings stored? I see a recursive function that returns a char and uses a variable called name which is apparently defined somewhere, but I don't know where. What is originally calling the rec function and how many times is rec non-recursively called from main or some other function?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: combination of strings

 
1
  #8
May 20th, 2008
Recursion and iteration are two sides of the same coin.

The difference is, in C++, you have the potential of blowing your stack. So on Windows, if you have more than sizeof(Available RAM + Swap Space)/sizeof(Stack Frame) permutations, you'll crash your computer. Even if you don't get that high though... once you exceed RAM size your program will begin to slow considerably.

All that aside, recursion keeps state ("remembers" things) by passing and returning values.

As a simple example, the Fibonacci sequence is defined as:
F(0) = 0
F(1) = 1
F(n) = F(n-1) + F(n-2)

A simple iterative solution might look something like
  1. unsigned long long i_fib( unsigned n )
  2. {
  3. switch (n)
  4. {
  5. case 0: return 0;
  6. case 1: return 1;
  7. default:
  8. unsigned long long f_of_n_minus_1 = 1;
  9. unsigned long long f_of_n_minus_2 = 0;
  10. unsigned long long f_of_n = 1;
  11. for (; n > 1; n--)
  12. {
  13. f_of_n = f_of_n_minus_1 + f_of_n_minus_2;
  14. f_of_n_minus_2 = f_of_n_minus_1;
  15. f_of_n_minus_1 = f_of_n;
  16. }
  17. return f_of_n;
  18. }
  19. }

To make that recursive, we have to identify our state variables: F(n-1), F(n-2), F(n), and of course, n.
The current sum is dependent on the previous iterations, so that should be a return value, while everything else is dependent on 'n'.

So, that same routine in recursion would be
  1. unsigned long long r_fib( unsigned n )
  2. {
  3. switch (n)
  4. {
  5. case 0: return 0;
  6. case 1: return 1;
  7. default: return r_fib(n-1) + r_fib(n-2)
  8. }
  9. }

While this looks prettier, it actually causes a problem: each call to r_fib() generates two additional calls. So the number of times it is called grows exponentially as 'n' gets larger (and threatens to take us to see the BSOD).

So, lets rethink again. The iterative version is very small and fast, whereas the recursive version explodes.
If you look closely, you'll see that the reason is that we essentially compute the value of F(n-1) twice for each call.
We'd sure like to use the power of addition from the iterative version in our recursive version.

You can always stop here and think about it a moment, or just keep on reading.
  1. unsigned long long fib(
  2. unsigned n,
  3. unsigned long long f_n_minus_1 = 1,
  4. unsigned long long f_n_minus_2 = 0
  5. ) {
  6. switch (n)
  7. {
  8. case 0:
  9. return f_n_minus_2;
  10. // case 1:
  11. // return f_n_minus_1;
  12. default:
  13. unsigned long long f_n = f_n_minus_1 + f_n_minus_2;
  14. return fib( n-1, f_n, f_n_minus_1 );
  15. }
  16. }
In this case, we've kept our additive state in the arguments and done something tricky. We reversed the role of 'n': we count backwards up to F(n). You'll also notice how the final number accumulates into 'f_n_minus_2', so we can just return that. Finally, case 1 becomes completely unnecessary.
I'm sorry if this last one is a little tough, but you'll have to use inductive reasoning to see it. (I'm running out of time so I'm going to wrap up.)


So, why all this post for your problem?
Because it bears directly upon the solution. As I indicated the last time, you can easily generate permutations by counting and modulus. This lends itself to an iterative approach. But, since recursion and iteration are so closely related, you can move things around the same way to make the routine both recursive and efficient.

Identify your state variables, and whether they are dependent on the next iteration or not, then put them in your argument list.

Hope this helps.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC