Thread: Question on permutated indices

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    Question on permutated indices

    I'm doing an example from the book which I have to create a permuted permute the index, the book says it takes a sentence (in this case two), ad this is the in/output

    Code:
     
    The quick brown fox
    jumped over the fence
    
    [b]output[b]
          The quick   brown fox
    jumped over the   fence
    the quick brown   fox
                      jumped over the fence
    
    etc..
    
    The algorithm in the book suggests I do two things which i have, which is extract the sentence and then make rotations, I did so with these functions

    Code:
    #include "functions.h"
    #include <cctype>
    #include <sstream>
    
    using std::vector;
    using std::string;
    using std::istringstream;
    
    typedef vector<string>::size_type str_sz;
    
    vector<string> parse(const string& aStr)
    {
        istringstream words(aStr);
        string temp;
        vector<string> parsed;
        
        while(words >> temp)
            parsed.push_back(temp);
                   
        return parsed;
    }
    
    vector<string> rotations (const string& aStr)
    {
        
        vector<string> parsed = parse(aStr);
        str_sz words = parsed.size();
        
        vector<string> permutations;    
        string final, first, temp;
        
        permutations.push_back(aStr); //puts the first string from the start
        temp = aStr;
        
        //does nubmer of rotations as there are words(excluding  one for first sentence)
        for(str_sz i = 0; i < words - 1; i++)  
        {
            istringstream body(temp); //sets the sstream object to the temp string
            
            body >> first;   //holds the first word of the sstream
            
            while(body >> temp)
                final += temp + " ";  //concatenates the rest of the sstream
            
            final += first;  //puts the first word at the end
            
            permutations.push_back(final);  //puts that rotation into 
            
            temp = final;    //sets temp
            final = "";  
        }  
        return permutations; 
    }
    The first function just parses the words, I use that for the sake of getting the number of words so I can do a certain number of rotations. Now the second function gets the string into a istringstream and then gets the first word into a string variable, and the rest into another string variable, the concatenates the body with the first word to create one rotation and puts that into an array. It then creates a new istringstream with the string value of the last rotation. It works fine.

    the next thing is to sort the rotations, which I assume is already done since I pushed them back in the array in the order I rotated them

    then this is where I'm confused...it says I should "unrotate and write the permuted index, which involves finding the separator, putting the phrase back together, and writing it propperly formatted"

    Maybe it's because I'm not familiar with what a permuted index is (though I understand the concept of permutations), that I'm having trouble with this. If someone could re-word it so I can wrap my head around what I am to do, it would be helpful.
    Last edited by indigo0086; 05-04-2006 at 04:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM