| | |
combination of strings
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 52
Reputation:
Solved Threads: 1
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:
PLZ help me as I am learning recursion by myself and its a very hard topic.......
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:
C++ Syntax (Toggle Plain Text)
char rec(string in[],vector<int> brand,int i,int j,int l) { if(j==l){ j=0; name+=rec(in,brand,++i,++j,l); --l; return in[brand[i]][j]; } if(l==-1) { l=2;j=0;i+=1; } name+=rec(in,brand,i,++j,l); }
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.
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.
The modulus doesn't need to be ten. It can be anything you want, like three.
That should be enough to get you started.
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.
C++ Syntax (Toggle Plain Text)
// Print 0123456789... ad infinitum for (unsigned u = 0; ; u++) 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.
C++ Syntax (Toggle Plain Text)
// Print abcdefghij... ad infinitum char letters[] = "abcdefghij"; for (unsigned u = 0; ; u++) 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
•
•
Join Date: Jul 2007
Posts: 30
Reputation:
Solved Threads: 0
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...
http://www.cplusplus.com/reference/a...rmutation.html
Hope this helps...
•
•
Join Date: Jan 2008
Posts: 52
Reputation:
Solved Threads: 1
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
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.
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
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
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
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
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.
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.
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
C++ Syntax (Toggle Plain Text)
unsigned long long i_fib( unsigned n ) { switch (n) { case 0: return 0; case 1: return 1; default: unsigned long long f_of_n_minus_1 = 1; unsigned long long f_of_n_minus_2 = 0; unsigned long long f_of_n = 1; for (; n > 1; n--) { f_of_n = f_of_n_minus_1 + f_of_n_minus_2; f_of_n_minus_2 = f_of_n_minus_1; f_of_n_minus_1 = f_of_n; } return f_of_n; } }
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
C++ Syntax (Toggle Plain Text)
unsigned long long r_fib( unsigned n ) { switch (n) { case 0: return 0; case 1: return 1; default: return r_fib(n-1) + r_fib(n-2) } }
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.
C++ Syntax (Toggle Plain Text)
unsigned long long fib( unsigned n, unsigned long long f_n_minus_1 = 1, unsigned long long f_n_minus_2 = 0 ) { switch (n) { case 0: return f_n_minus_2; // case 1: // return f_n_minus_1; default: unsigned long long f_n = f_n_minus_1 + f_n_minus_2; return fib( n-1, f_n, f_n_minus_1 ); } }
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.
![]() |
Similar Threads
- does sort of algorithm work for strings (C++)
- Strings and the new operator? (C++)
- Help with Creating a Command Line Menu (C++)
- sorting a text file (C++)
- String class methods... (Java)
- Desiging a set of rules for a match (C++)
- Arizona Web wants reviews too! (Website Reviews)
- C++ Syntax (C++)
- JSP and Oracle (JSP)
Other Threads in the C++ Forum
- Previous Thread: how to create a Linux distro independent code
- Next Thread: Dealing with verilog format ?...
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy desktop directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






