473,386 Members | 1,828 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,386 software developers and data experts.

string permutation function

I am trying to write two recursive functions involving two strings, 1)CAT & 2)MAN.

My function needs to print out:
TACMAN
ATCMAN
CTAMAN
TCAMAN
ACTMAN
CATMAN

Two functions required are:
1) void string::insert(size_type position, size_type number_of_copies, char c);
2) void string::erase(size_type position, size_type n)

Other functions I may need:
3) Do I need to use a permute function and a dopermute function? How do I make the permute functions part of my program by using insert and erase?

Expand|Select|Wrap|Line Numbers
  1. using namespace std;
  2.  
  3. void string::insert(size_type position, size_type number_of_copies, char c)
  4. {
  5.     string s1 = "CAT";
  6.     string s2 = "MAN";
  7.  
  8.         //add or insert CAT to first 3
  9.         //add or insert MAN to last 3
  10.             //then...
  11.                 //...add TA and insert with CMAN = "TACMAN"
  12.                 //..replace TA with AT and insert with CMAN = "ATCMAN"
  13.                 //..call erase c();
  14.                 //add CT and insert with AMAN = "CTAMAN"
  15.                     //..replace CT with TC and insert with AMAN = "TCAMAN"
  16.                         //..call erase c();
  17.                         //add AC and insert with TMAN = "ACTMAN"
  18.                         //..replace AC with CA and insert with TMAN = "CATMAN"
  19.  
  20.  
  21. //postcondition: The specified number of copies of c have been 
  22. //inserted into the string at the indicated position. 
  23. //Existing characters that used to be at or after the given position have been shifted right 
  24. //one spot 
  25. }
  26. void string::erase(size_type position, size_type n)
  27. {
  28.  
  29.     // delete c[]; or c[] = NULL;
  30. }
  31.  
  32.  
  33. int main()
  34. {
  35.     char c[5];
  36.  
  37.  
  38.   insert(,,); //First function call, so it starts at one        
  39. }
  40.  
Please give me ideas and suggestions. I need help in putting my idea into code as this is all new to me.

Thank you.
Nov 13 '07 #1
2 2963
RRick
463 Expert 256MB
The key ideas here are: permutation and recursion. This means your insert routine must call itself. In your example, it doesn't. Take a look at the visit routine in the following link for an idea of what this means. http://www.bearcave.com/random_hacks/permute.html

In your example, only the first string is being permutated. The second one is simply added to the end of the various permutations. One idea is to keep these separate until you need to print them.

As to why you need two recursive functions, this seems to be based on a specific type of permutation algorithm. I'm not sure why you need to do this. Once again, the above link shows an example.
Nov 13 '07 #2
I have figured out the "permutation" part and I need help with "recursion" part.

Here are the conditions for my functions:

1) the stopping case of the function occurs when the length of first has zero characters

2) for void string::insert:
-The specified number of copies of c have been inserted into the string at the indicated position.
-Existing characters that used to be at or after the given position have been shifted right one spot.

3) for void string::erase:
-n characters have been removed from the string, beginning at the specified position.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string.h>
  4. #include <string>
  5. #include <iomanip> 
  6.  
  7. using namespace std;
  8.  
  9. /*void string::insert(size_type position, size_type number_of_copies, char c)
  10. {
  11.     string string1 = "CAT";
  12.     string string2 = "MAN";
  13.         permute ("", "CAT");
  14.         permute ("", "MAN");
  15.     cout<<string1 + string2; //results in error.
  16.                                                     //how to use insert?
  17.  
  18.  
  19. }
  20. void string::erase(size_type position, size_type n)
  21. {
  22.     //dont know what to do
  23. }*/
  24. void permute( string prefix, string s )
  25. {
  26.     if ( s.size() <= 1  )
  27.         cout <<prefix <<s <<"\n";
  28.     else
  29.         for ( char *p = s.begin(); p <s.end(); p++ )
  30.         {
  31.             char c = *p;
  32.             s.erase( p );
  33.             permute( prefix + c, s );
  34.             s.insert( p, c );
  35.         }
  36.  
  37. }
  38. int main()
  39. {
  40.     permute( "CAT", "MAN");
  41.     return 0;      
  42. }
  43.  
Please help me! I am drowning!
Nov 14 '07 #3

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

Similar topics

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 =...
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
3
by: Csaba Gabor | last post by:
I'm comparing the text of (snippets of) web pages which I expect to be quite different or quite similar. In the case where they are similar, I would like to display the more recent one and say...
20
by: anurag | last post by:
hey can anyone help me in writing a code in c (function) that prints all permutations of a string.please help
12
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way...
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...
2
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
16
by: saki | last post by:
Write a program to print all the permutations of a given string.
13
by: sillyhat | last post by:
Hello, can someone please help. I found the following code at http://code.activestate.com/recipes/252178/ def all_perms(str): if len(str) <=1: yield str else: for perm in all_perms(str):...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.