473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help with Permutation

25
Create a program to display all of the permutations of a string of any length. A permutation is a re-arrangement of the order of letters. For example, the string "abc" has permutations "acb", "bca", "bac", "cab", and "cba"

Here is my code but I don't know how to re-arrangement. Please help me

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>                 //Required if your program does any I/O
  2. #include <string>                   //Required if your program uses C++ strings
  3. #include <algorithm>                // next_permutation() via stl_algo.h
  4. #include <vector>                   // stl vector header
  5. #include <iterator>                 // ostream_iterator
  6. using namespace std;                //Required for ANSI C++ 1998 standard.
  7.  
  8.  
  9. int main (int argc, char **argv)    //Arguments to main are command line arguments
  10. {
  11.     char reply;                     //A character variable to hold user input
  12.     string something;
  13.     vector<char> iV;
  14.  
  15.  
  16.     cout << "Enter a string: ";
  17.     cin >> something;
  18.  
  19.     //Take every single letter in the string and put it into vector
  20.     for(int x = 0; x < something.length(); x++)
  21.     {
  22.           iV.push_back(something[x]);
  23.     }
  24.  
  25.     // display original
  26.     cout << "\nOriginal string:\n";
  27.     copy(iV.begin(),iV.end(),ostream_iterator<char>(cout,""));
  28.  
  29.     cout << "\n\nPermutations:\n";
  30.     // loop until all permutations are generated and displayed
  31.     // does not display original string
  32.     while (next_permutation(iV.begin(), iV.end())) 
  33.     { 
  34.             copy(iV.begin(),iV.end(),ostream_iterator<char>(cout,""));
  35.             cout << endl;
  36.     }
  37.  
  38.     // This section stops the program 'flashing' off the screen.
  39.     cout << "\n\nPress q (or any other key) followed by 'Enter' to quit: ";
  40.     cin >> reply;
  41.     return 0;
  42. }
  43.  
Mar 17 '07 #1
8 2497
wazzup
25
This is my output:

Expand|Select|Wrap|Line Numbers
  1. Enter a string: abc
  2.  
  3. Original string:
  4. abc
  5.  
  6. Permutations:
  7. acb
  8. bac
  9. bca
  10. cab
  11. cba
  12.  
  13.  
  14. Press q (or any other key) followed by 'Enter' to quit:
Mar 17 '07 #2
Ganon11
3,652 Expert 2GB
So what are you still trying to do? It looks like you have correctly solved the problem to me. Try testing it with some other strings to see what output you get.
Mar 17 '07 #3
wazzup
25
With "abc" input

My output is: "acb", "bac", "bca", "cab" and "cba"

But it should be like this: "acb", "bca", "bac", "cab", and "cba"
Mar 17 '07 #4
Ganon11
3,652 Expert 2GB
Does it really matter? You were asked to display the possible permutations of the input string, and you have - the order in which they are displayed should not be an issue.
Mar 17 '07 #5
Create a program to display all of the permutations of a string of any length. A permutation is a re-arrangement of the order of letters. For example, the string "abc" has permutations "acb", "bca", "bac", "cab", and "cba"

Here is my code but I don't know how to re-arrangement. Please help me

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>                 //Required if your program does any I/O
  2. #include <string>                   //Required if your program uses C++ strings
  3. #include <algorithm>                // next_permutation() via stl_algo.h
  4. #include <vector>                   // stl vector header
  5. #include <iterator>                 // ostream_iterator
  6. using namespace std;                //Required for ANSI C++ 1998 standard.
  7.  
  8.  
  9. int main (int argc, char **argv)    //Arguments to main are command line arguments
  10. {
  11.     char reply;                     //A character variable to hold user input
  12.     string something;
  13.     vector<char> iV;
  14.  
  15.  
  16.     cout << "Enter a string: ";
  17.     cin >> something;
  18.  
  19.     //Take every single letter in the string and put it into vector
  20.     for(int x = 0; x < something.length(); x++)
  21.     {
  22.           iV.push_back(something[x]);
  23.     }
  24.  
  25.     // display original
  26.     cout << "\nOriginal string:\n";
  27.     copy(iV.begin(),iV.end(),ostream_iterator<char>(cout,""));
  28.  
  29.     cout << "\n\nPermutations:\n";
  30.     // loop until all permutations are generated and displayed
  31.     // does not display original string
  32.     while (next_permutation(iV.begin(), iV.end())) 
  33.     { 
  34.             copy(iV.begin(),iV.end(),ostream_iterator<char>(cout,""));
  35.             cout << endl;
  36.     }
  37.  
  38.     // This section stops the program 'flashing' off the screen.
  39.     cout << "\n\nPress q (or any other key) followed by 'Enter' to quit: ";
  40.     cin >> reply;
  41.     return 0;
  42. }
  43.  







hi i executed this pgm i got correct result when i give 3 character like abc,cba...... but i got incorrect result when i gave more than three character like dine,jkkl.... pls clarify my doubt
Apr 12 '07 #6
nmadct
83 Expert
From this documentation of next_permutation --

http://www.sgi.com/tech/stl/next_permutation.html

-- it seems that the function might not work if your original series is not sorted in non-descending order.

It says, "There is a finite number of distinct permutations (at most N! [1], where N is last - first), so, if the permutations are ordered by lexicographical_compare, there is an unambiguous definition of which permutation is lexicographically next."

My tests of your code showed that it worked right except for your example "dllk". Try sorting that ("dkll") in the code, and see how it works then. My tests indicate that it works right if you enter "dkll".
Apr 12 '07 #7
nmadct
83 Expert
Re-examining the output, I see that this is the case. The next_permutation function assumes it has finished when the series is in reverse order. (Otherwise it wouldn't know where to stop.) Thus, if you don't start with an ordered series, some of the permutations will be skipped.
Apr 12 '07 #8
wwx
2
There are some different algorithms to generate permutation. The common used algorithm is Dictionary Based Method which produce all the permutation in order. So your problem is to find the appropriate method for the permutation which can produce the sequence as you though. Here is a very fast example of permutation that I found on the net :
http://ongkowijaya.googlepages.com/O...itudeTests.htm , and it is originally by Johnson et al. I show you this to let you know that your question is a part of the Mathematic World.
Dec 8 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: drs | last post by:
Hi, I am trying to find all lists of length x with elements a, b, and c. To this end, I have created the class below, but it is not quite working and I am having trouble figuring out what to...
6
by: Jim Davidson | last post by:
I'm teaching myself javascript and need some help with this code. can someone tell me what's wrong with it? function getRandomNo() { while (true) { nImgNo = Math.round(Math.random() * 10); var...
10
by: Talin | last post by:
I'm sure I am not the first person to do this, but I wanted to share this: a generator which returns all permutations of a list: def permute( lst ): if len( lst ) == 1: yield lst else: head =...
3
by: Jack Middleton | last post by:
Hi! I'm lookin for a faster permutation algorithm for matrices. I know that it can be done with multiplying a matrix with a permutation matrix. It just seems a waste to iterate through all those...
1
by: user | last post by:
Hello I have Array of 50 ints. I want to receive random permutation, so in each int will be different number from 0-49. Is there any class for permutation ? Thanx Michal
27
by: onkar | last post by:
How to generate different permutations of n char array? ex : for n= 3, and basic string = abc bca cab bac cab ..... ... ..
6
by: fool | last post by:
Dear group, Given a string I have to print the permutation, using some looping tricks. This is not a Home work problem. My best try as beginner is: #include<stdio.h> #include<stdlib.h> ...
3
by: weidongtom | last post by:
Hi, I have been working at this problem, and I think I need a permutation algorithm that does the following: Given a list of elements that are either a character or a character follows by a...
0
by: 249740 | last post by:
Write a program that reads N phrases and determines if one phrase is a permutation of the other. For example: Phrase 1 is: “One World One Dream” Phrase 2 is: “World One One Dream”. Then the output...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.