Advertisement
Guest User

03_Task3_Code.cpp

a guest
Jul 15th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. // 01_03_Task3_Code.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream> //for std::cout
  6. #include <string>
  7. #include <sstream>
  8. #include <iomanip>
  9. #include <vector>  
  10. #include <vector>  
  11. #include <utility>  //pair<vey, value>
  12.  
  13. using namespace std;
  14.  
  15. int main()
  16. {
  17.     cin.sync_with_stdio(false);
  18.     cout.sync_with_stdio(false);
  19.  
  20.     string line;
  21.     getline(cin, line);
  22.     vector<string> data_V_strSeparators;
  23.     string strToken;
  24.     istringstream iss(line);
  25.     while (iss >> strToken)
  26.     {
  27.         data_V_strSeparators.push_back(strToken);
  28.     }
  29.     string text;
  30.     getline(cin, text);
  31.  
  32.     for (auto n : data_V_strSeparators)
  33.     {
  34.         int pos = text.find(n);
  35.  
  36.         while (pos != string::npos)
  37.         {
  38.             text.replace(pos, n.size(), "*");
  39.             pos = text.find(n, pos + 1);
  40.         }
  41.     }
  42.     //CHECK:
  43.     //cout << text << endl;
  44.  
  45.     text += "*";
  46.     string strNumToFind;
  47.     getline(cin, strNumToFind);
  48.     int cnt = 0;
  49.     while (strNumToFind != "0")
  50.     {
  51.         int firstIndexOfNumberToFind = text.find(strNumToFind, 0);
  52.         if (firstIndexOfNumberToFind == string::npos)
  53.         {
  54.             cout << 0 << endl;
  55.             getline(cin, strNumToFind);
  56.             continue;
  57.         }
  58.         int firstIndexOfStar = text.find("*", 0);
  59.  
  60.         if (firstIndexOfNumberToFind < firstIndexOfStar)
  61.         {
  62.             cnt = 0;
  63.             int indexOfOccurence = firstIndexOfNumberToFind;
  64.             cnt++;
  65.             while (indexOfOccurence != string::npos)
  66.             {
  67.                 indexOfOccurence = text.find("*", indexOfOccurence + strNumToFind.size());
  68.                 indexOfOccurence = text.find(strNumToFind, indexOfOccurence + 1);
  69.                 if (indexOfOccurence != string::npos)
  70.                 {
  71.                     cnt++;
  72.                 }
  73.                 else
  74.                 {
  75.                     cout << cnt << endl;
  76.                     break;
  77.                 }
  78.             }
  79.         }
  80.         else
  81.         {
  82.             cnt = 0;
  83.             int indexOfOccurence = firstIndexOfStar;
  84.             while (indexOfOccurence != string::npos)
  85.             {
  86.                 indexOfOccurence = text.find(strNumToFind, indexOfOccurence + 1);
  87.                 if (indexOfOccurence != string::npos) {
  88.                     cnt++;
  89.                 }
  90.                 else
  91.                 {
  92.                     cout << cnt << endl;
  93.                     break;
  94.                 }
  95.                 indexOfOccurence = text.find("*", indexOfOccurence + strNumToFind.size());
  96.                 //CHECK:
  97.                 //cout << "count: " << cnt << "Index of *: " << indexOfOccurence << endl;
  98.             }
  99.         }
  100.         getline(cin, strNumToFind);
  101.     }
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement