Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- #include<vector>
- #include<sstream>
- #include<cctype>
- #include<algorithm>
- #include<list>
- std::vector<std::string> readInput() {
- std::vector<std::string> vec{};
- std::string line;
- getline(std::cin, line);
- std::istringstream istr(line);
- std::string word;
- while (istr >> word)
- {
- vec.push_back(word);
- }
- return vec;
- }
- void swap(std::string& str1, std::string& str2) {
- std::string oldStr1 = str1;
- str1 = str2;
- str2 = oldStr1;
- }
- std::vector<std::string> erasePuntuations(std::vector<std::string>& sentence,
- std::vector<int>& getPunctIdx,
- std::vector<char>& getPuncts) {
- size_t size = sentence.size();
- for (size_t j = 0; j < size; ++j) {
- std::string currWord = sentence[j];
- if (ispunct(currWord[currWord.size() - 1]) && currWord[currWord.size() - 1] != '+')
- {
- getPunctIdx.push_back(j);
- getPuncts.push_back(currWord[currWord.size() - 1]);
- sentence[j].pop_back();
- }
- }
- return sentence;
- }
- size_t getIndex(std::vector<std::string>& noPunctSentence, std::string& str) {
- std::vector<std::string>::const_iterator it =
- std::find(noPunctSentence.begin(), noPunctSentence.end(), str);
- return it - noPunctSentence.begin();
- }
- std::vector<std::string> reverseWords(std::vector<std::string>& noPunctSentence) {
- size_t size = noPunctSentence.size();
- int counterswaps = 0;
- for (size_t j = 0; j < size; ++j) {
- bool foundEqualSizeWord = true;
- if (j > 0)
- {
- int counterRepetitions = 0;
- for (int k = j - 1; k >= 0; k--)
- {
- if (noPunctSentence[j].size() == noPunctSentence[k].size())
- {
- counterRepetitions++;
- }
- }
- if (counterRepetitions != 0)
- {
- int counterEqualRep = 0;
- for (int i = size - 1; i >= 0; i--) {
- if (noPunctSentence[j].size() == noPunctSentence[i].size())
- {
- if (j == i)
- {
- foundEqualSizeWord = false;
- break;
- }
- counterEqualRep++;
- if (counterEqualRep == counterRepetitions)
- {
- size = getIndex(noPunctSentence, noPunctSentence[i]) - 1;
- std::cout << "start searching from index: " << size << std::endl;
- break;
- }
- }
- }
- if (!foundEqualSizeWord)
- {
- continue;
- }
- }
- else
- {
- size = noPunctSentence.size();
- }
- }
- for (int i = size - 1; i >= 0; i--)
- {
- if (noPunctSentence[j].size() == noPunctSentence[i].size() && i > j)
- {
- swap(noPunctSentence[j], noPunctSentence[i]);
- std::cout << noPunctSentence[i] << "<->" << noPunctSentence[j] << std::endl;
- counterswaps++;
- break;
- }
- }
- continue;
- }
- std::cout << "swaps: " << counterswaps << std::endl;
- return noPunctSentence;
- }
- std::vector<std::string> getPunctToReverseSentence(const std::vector<int>& getPunctIdx,
- const std::vector<char>& getPuncts,
- std::vector<std::string>& reverseSentence) {
- size_t size = getPunctIdx.size();
- for (size_t i = 0; i < size; i++)
- {
- reverseSentence[getPunctIdx[i]].push_back(getPuncts[i]);
- }
- return reverseSentence;
- }
- std::string getPosOfUpperAlpha(std::vector<std::string>& reverseSentenceWithPunct) {
- std::string reverseSentence;
- for (const std::string word : reverseSentenceWithPunct) {
- reverseSentence += ' ' + word;
- }
- reverseSentence = reverseSentence.erase(0, 1);
- for (char& c : reverseSentence) {
- if (isalpha(c) && isupper(c))
- {
- c = tolower(c);
- }
- }
- reverseSentence[0] = toupper(reverseSentence[0]);
- return reverseSentence;
- }
- //void printSolution(const std::vector<std::string>& solution) {
- // for (const std::string word : solution) {
- // std::cout << word << ' ';
- // }
- // std::cout << std::endl;
- //}
- int main() {
- std::vector<std::string> sentence = readInput();
- std::vector<int> getPunctIdx;
- std::vector<char> getPuncts;
- std::vector<std::string> noPunctSentence = erasePuntuations(sentence, getPunctIdx, getPuncts);
- std::vector<std::string> reverseSentence = reverseWords(noPunctSentence);
- for (const std::string word : reverseSentence) {
- std::cout << word << ' ';
- }
- std::cout << std::endl;
- for (const int punctIdx : getPunctIdx) {
- std::cout << punctIdx << ' ';
- }
- std::cout << std::endl;
- for (const char c : getPuncts) {
- std::cout << c << ' ';
- }
- std::cout << std::endl;
- std::vector<std::string> reverseSentenceWithPunct = getPunctToReverseSentence(getPunctIdx, getPuncts, reverseSentence);
- for (const std::string word : reverseSentenceWithPunct) {
- std::cout << word << ' ';
- }
- std::cout << std::endl;
- std::string solution = getPosOfUpperAlpha(reverseSentenceWithPunct);
- std::cout << solution << std::endl;
- //printSolution(solution);
- return 0;
- }
Add Comment
Please, Sign In to add comment