0

I am trying to implement a program that produces a permuted index. Acctualy it is this excercise : What is a permuted index?

I have already wrote a function that makes a rotations on strings inside a vector of strings but I do not know how to save how many rotations were on each string to be able to unrotate it later for the same number of rotations. Actually I have function that splits a sentence into words and function that generates a rotations:

#include <iostream>
#include <vector>

using namespace std;


vector<string> bookImplementation(const vector<string> &splitted) {
    vector<string> result;
    result = generateRotations(splitted);
// WHAT NEXT?

}
vector<string> split(const string &s) {
    vector<string> ret;
    string::size_type i = 0;
    while (i != s.size()) {
        while (i != s.size() && isspace(s[i]))
            ++i;
        string::size_type j = i;
        while (j != s.size() && !isspace(s[j]))
            j++;
        if (i != j) {
            ret.push_back(s.substr(i, j - i));
            i = j;
        }
    }
    return ret;
}

vector<string> generateRotations(const vector<string> &splitted) {
    vector<string> result;
    for (vector<string>::size_type i = 0; i != splitted.size(); ++i) {
        string oneLine;
        vector<string> temp(splitted);

//HOW TO SAVE NUMBER OF ROTATIONS (i)?

        temp.insert(temp.begin(), temp.end() - i, temp.end());
        temp.erase(temp.end() - i, temp.end());

        for (vector<string>::size_type j = 0; j != temp.size(); ++j) {
            oneLine += " ";
            oneLine += temp[j];
        }
        result.push_back(oneLine);
    }
    return result;
}

int main() {
    string phrase;
    cout << "Please give me some phrase" << endl;
    getline(cin, phrase);

    vector <string> splitted = split(phrase);

    vector<string> permuted = bookImplementation(splitted);

    for (const auto i : permuted) {
        cout << i << endl;
    }

    return 0;
}

And would be nice if somebody tell me if I do anything wrong.

Community
  • 1
  • 1
Mateusz
  • 604
  • 8
  • 20

1 Answers1

0

With out speaking to the correctness of the algorithm, in order to save the permutation value i, and return it from your function generateRotations(..) you are going to have to create a structure to hold it. Perhaps instead of returning std::vector< std::string > from generateRotations you could return a vector of structures which contain both your string and its permutation number i.

struct string_with_perm
{
    std::string str;
    int perm;
};

Then change the function to be like ...

vector<string_with_perm> generateRotations(const vector<string> &splitted) {
    vector<string_with_perm> result;
    for (vector<string>::size_type i = 0; i != splitted.size(); ++i) {
        string oneLine;
        vector<string> temp(splitted);

//HOW TO SAVE NUMBER OF ROTATIONS (i)? NOW SAVED BELOW NEAR LAST LINE

        temp.insert(temp.begin(), temp.end() - i, temp.end());
        temp.erase(temp.end() - i, temp.end());
        for (vector<string>::size_type j = 0; j != temp.size(); ++j) {
            oneLine += " ";
            oneLine += temp[j];
        }
        string_with_perm sp;
        sp.str = oneLine;
        sp.perm = i;
        result.push_back(sp);
    }
    return result;
}
JimmyNJ
  • 1,134
  • 1
  • 8
  • 23
  • yeah that is my only thought on that task and I sarted coding the same as you wrote, but in book there wasn't any structures mentioned yet, so I guess that could be done with out structs. – Mateusz Mar 08 '16 at 21:42