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.