Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <vector>
- #include <string>
- using namespace std;
- string ReadLine() {
- string s;
- getline(cin, s);
- return s;
- }
- int ReadLineWithNumber() {
- int result;
- cin >> result;
- ReadLine();
- return result;
- }
- vector<string> SplitIntoWords(const string& text) {
- vector<string> words;
- string word;
- for (const char c : text) {
- if (c == ' ') {
- if (!word.empty()) {
- words.push_back(word);
- word.clear();
- }
- }
- else {
- word += c;
- }
- }
- if (!word.empty()) {
- words.push_back(word);
- }
- return words;
- }
- int main() {
- const int queryCount = ReadLineWithNumber();
- /*Проблема в коде заключается в том, что он не учитывает возможное наличие пробелов в конце строк в векторе queries. При чтении строк с клавиатуры, конец строки ('\n') остается во входном буфере после считывания числа queryCount. Поэтому, когда вы считываете строки с помощью getline(cin, s), он сначала считывает пустую строку, оставшуюся после числа, и только затем начинает считывать фактические строки запросов.*/
- cin.ignore(); // Очистка символа новой строки после числа
- vector<string> queries(queryCount);
- for (string& query : queries) {
- query = ReadLine();
- }
- const string buzzword = ReadLine();
- cout << count_if(queries.begin(), queries.end(), [buzzword](const string& query) {
- const vector<string> query_words = SplitIntoWords(query);
- return count(query_words.begin(), query_words.end(), buzzword) != 0;
- });
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement