IoIiderp

Permuted Index

Jan 9th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.71 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cctype>
  3. #include <iostream>
  4. #include <iterator>
  5. #include <vector>
  6. #include <string>
  7. #include <sstream>
  8.  
  9. struct Word
  10. {
  11.     int originalindex;
  12.     std::string str;
  13. };
  14.  
  15.  
  16. /*
  17.     Returns all the words of a given string
  18. */
  19. std::vector<Word> getWords(const std::string &in);
  20.  
  21. /*
  22.     Returns a vector of rotated strings given in vectors of words
  23.     We rotate the strings by placing the first word at the end until it is fully rotated
  24. */
  25. std::vector< std::vector<Word> > rotateString(const std::string &in);
  26.  
  27. /*
  28.     Returns a generated string based on the words given.
  29. */
  30. std::string makeStringFromWords(const std::vector<Word> &in);
  31.  
  32.  
  33. int main()
  34. {
  35.     std::vector<std::string> input
  36.     {
  37.         "The quick brown fox",
  38.         "jumped over the fence"
  39.     };
  40.  
  41.     std::vector< std::vector<Word> > output;
  42.  
  43.     // Convert input to std::vector< std::vector<Word> > and put it in the output, all input sentences will be inside that vector
  44.     for(const std::string &s : input)
  45.     {
  46.         std::vector< std::vector<Word> > sentences = rotateString(s);
  47.         for(const std::vector<Word> &s2 : sentences)
  48.         {
  49.             output.push_back(s2);
  50.         }
  51.     }
  52.  
  53.     // Sort the output
  54.     std::sort(output.begin(), output.end(), [](const std::vector<Word> &a, const std::vector<Word> &b){
  55.         std::string str_a = makeStringFromWords(a);
  56.         std::string str_b = makeStringFromWords(b);
  57.  
  58.         std::transform(str_a.begin(), str_a.end(), str_a.begin(), tolower);
  59.         std::transform(str_b.begin(), str_b.end(), str_b.begin(), tolower);
  60.  
  61.         return str_a < str_b;
  62.     });
  63.  
  64.     // Split output in left and right
  65.     std::vector< std::vector<Word> > Left;
  66.     std::vector< std::vector<Word> > Right;
  67.  
  68.     for(const std::vector<Word> &s : output)
  69.     {
  70.         std::vector<Word> l_sentence;
  71.         std::vector<Word> r_sentence;
  72.  
  73.         for(size_t i=0; i<s.size(); ++i)
  74.         {
  75.             if(s[i].originalindex < (int)i)
  76.             {
  77.                 l_sentence.push_back(s[i]);
  78.             } else {
  79.                 r_sentence.push_back(s[i]);
  80.             }
  81.         }
  82.  
  83.         Left.push_back(l_sentence);
  84.         Right.push_back(r_sentence);
  85.     }
  86.  
  87.  
  88.     // Display the output
  89.     size_t left_max_len = 0;
  90.     for(const std::vector<Word> &s : Left)
  91.     {
  92.         left_max_len = std::max(left_max_len, makeStringFromWords(s).length());
  93.     }
  94.  
  95.     for(size_t i=0; i<Left.size(); ++i)
  96.     {
  97.         std::string leftstr = makeStringFromWords(Left[i]);
  98.         std::cout << std::string(left_max_len-leftstr.length(), ' ') << leftstr << std::string(5, ' ') << makeStringFromWords(Right[i]) << std::endl;
  99.     }
  100.  
  101.     return 0;
  102. }
  103.  
  104.  
  105. /*
  106.     Returns all the words of a given string
  107. */
  108. std::vector<Word> getWords(const std::string &in)
  109. {
  110.     // Make a vector of strings which have all the words of the given input string
  111.     std::istringstream iss(in);
  112.     std::vector<std::string> s_words // Construct a vector from 2 iterators
  113.     {
  114.         std::istream_iterator<std::string>{iss}, // iterator pointing to first element
  115.         std::istream_iterator<std::string>{} // end-of-stream iterator
  116.     };
  117.  
  118.     // Convert s_words
  119.     std::vector<Word> words;
  120.     for(std::string s : s_words)
  121.     {
  122.         Word w;
  123.         w.str = s;
  124.         words.push_back(w);
  125.     }
  126.  
  127.     return words;
  128. }
  129.  
  130. /*
  131.     Returns a vector of rotated strings given in vectors of words
  132.     We rotate the strings by placing the first word at the end until it is fully rotated
  133. */
  134. std::vector< std::vector<Word> > rotateString(const std::string &in)
  135. {
  136.     std::vector< std::vector<Word> > out;
  137.     std::vector<Word> words = getWords(in);
  138.  
  139.     // Loop through all the words so we have a vector of sentences with each sentence starting with i's word
  140.     for(size_t i=0; i<words.size(); ++i)
  141.     {
  142.         std::vector<Word> rotatedString;
  143.  
  144.         // Loop through all the words so we can form a sentence with all the words in the correct order
  145.         for(size_t r=0; r<words.size(); ++r)
  146.         {
  147.             int wordWeNeed = (i+r)%words.size(); // i makes sure each sentence starts with another word and r is meant to loop through the words
  148.             Word w = words[wordWeNeed];
  149.             w.originalindex = wordWeNeed;
  150.             rotatedString.push_back(w);
  151.         }
  152.  
  153.         out.push_back(rotatedString);
  154.     }
  155.  
  156.     return out;
  157. }
  158.  
  159. /*
  160.     Returns a generated string based on the words given.
  161. */
  162. std::string makeStringFromWords(const std::vector<Word> &in)
  163. {
  164.     std::string out;
  165.  
  166.     for(size_t i=0; i<in.size(); ++i)
  167.     {
  168.         out += in[i].str;
  169.  
  170.         if(i != in.size()-1)
  171.         {
  172.             out += ' ';
  173.         }
  174.     }
  175.  
  176.     return out;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment