DimovIvan

C++Advanced-Judge Assignment 1-Similarity

Jul 8th, 2021
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include<sstream>
  4. #include<cctype>
  5. #include<vector>
  6. #include<utility>
  7.  
  8. std::vector<std::string> removePunctsFromInput(std::string& input) {
  9.     std::vector<std::string> noPunctText;
  10.     for (size_t i = 0; i < input.size(); i++)
  11.     {
  12.         if (std::ispunct(input[i]))
  13.         {
  14.             input.replace(i, 1, " ");
  15.         }
  16.     }
  17.     //std::cout << input << std::endl;
  18.     std::istringstream istr(input);
  19.     std::string word;
  20.     while (istr >> word)
  21.     {
  22.         noPunctText.push_back(word);
  23.     }
  24.  
  25.     return noPunctText;
  26. }
  27.  
  28. std::pair<std::string, int> readPairFromInput() {
  29.     std::pair<std::string, int> pairInput;
  30.     std::cin >> pairInput.first >> pairInput.second;
  31.  
  32.     return pairInput;
  33. }
  34.  
  35. void printSolution(const std::vector<std::string>& noPunctText, const std::pair<std::string, int>& pairInput) {
  36.     const size_t vecSize = noPunctText.size();
  37.     const int compareWordSize = pairInput.first.size();
  38.     int counterWords = 0;
  39.     for (size_t i = 0; i < vecSize; i++)
  40.     {
  41.         const std::string currWord = noPunctText[i];
  42.         const int currWordSize = noPunctText[i].size();
  43.        // std::cout << currWordSize << std::endl;
  44.         if (currWordSize != compareWordSize)
  45.         {
  46.             continue;
  47.         }
  48.         else if (noPunctText[i][0] != pairInput.first[0])
  49.         {
  50.             continue;
  51.         }
  52.         else
  53.         {
  54.             int counterLettersMatch = 0;
  55.             for (size_t j = 0; j < currWordSize; j++)
  56.             {
  57.                 if (noPunctText[i][j] == pairInput.first[j])
  58.                 {
  59.                     counterLettersMatch++;
  60.                 }
  61.             }
  62.             //std::cout << counterLettersMatch << std::endl;
  63.             const double percentLettersMatch = ((counterLettersMatch * 1.0) / compareWordSize) * 100;
  64.            // std::cout << percentLettersMatch << std::endl;
  65.             if ((int)percentLettersMatch >= pairInput.second)
  66.             {
  67.                 counterWords++;
  68.             }
  69.         }
  70.     }
  71.     std::cout << counterWords << std::endl;
  72. }
  73.  
  74. int main() {
  75.     std::string text;
  76.     getline(std::cin, text);
  77.     const std::vector<std::string> noPunctText = removePunctsFromInput(text);
  78.     const std::pair<std::string, int> pairInput = readPairFromInput();
  79.     printSolution(noPunctText, pairInput);
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment