Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

จำนวนคำที่มีอยู่ในทุกประโยคที่กำหนดใน C++

เราได้รับหลายประโยคในรูปแบบสตริง เป้าหมายคือนับจำนวนคำที่มีอยู่ในทุกประโยค

หมายเหตุ - จะพิจารณาเฉพาะคำที่ประกอบด้วยตัวอักษรพิมพ์เล็กทั้งหมด

ถ้าประโยคคือ -

"I am learning C language"

"learning new things is easy"

"Kids are learning healthy habits"

มีแค่ "learning" ที่อยู่ในทั้งสาม จำนวนคือ 1

ตัวอย่าง

อินพุต - "The clothes were dry", "All the kids were playing", "Those were the best days"

เอาต์พุต - จำนวนคำที่มีอยู่ในทุกประโยคที่กำหนดคือ - 2

คำอธิบาย - คำ "the" และ "were" อยู่ในทุกประโยค

อินพุต - "We are going to school", "If you are willing then continue", "All these are sold"

เอาต์พุต - จำนวนคำที่มีอยู่ในทุกประโยคที่กำหนดคือ - 1

คำอธิบาย - คำ "are" อยู่ในทุกประโยค

แนวทางที่ใช้ในโปรแกรมด้านล่าง

ในแนวทางนี้ เราจะเก็บคำของประโยคแรกใน vector<pair<string, bool>> set ก่อน

เราจะหาคำใน vector set ในประโยคอื่นๆ ทั้งหมดโดยใช้ unordered_map<string, bool> check

  • รับ vector<string> vec และเริ่มต้นด้วยสตริงทั้งหมดที่มีประโยค
  • จำนวนประโยคจะเป็น vec.size()
  • ฟังก์ชัน words_sentences(vector<string> vec, int size) รับ vector ของประโยคและขนาด คืนค่าจำนวนคำที่มีอยู่ในทุกประโยคที่กำหนด
  • ตั้งค่า count เริ่มต้นเป็น 0
  • ใช้สตริงชั่วคราว str เก็บคำแต่ละคำในประโยค
  • เดินทางผ่านประโยคแรกที่เก็บใน vec[0] โดยใช้ลูป while
  • ภายในใช้ลูป while อีกอัน ดึงคำแต่ละคำใน str[] จนกว่าจะเจอ space
  • ตอนนี้เรามีคำของประโยคแรกใน str เพิ่มคู่ (str,true) เข้า set
  • ทำซ้ำสำหรับคำทั้งหมดในประโยคที่เก็บใน vec[0]
  • Vector set ตอนนี้มีคู่ของคำทั้งหมดของประโยคแรกพร้อมค่า true
  • เดินทางผ่าน vector ของประโยคจากประโยคที่ 2 ถึงประโยคสุดท้ายโดยใช้ลูป for จาก j=1 ถึง j
  • ดึงคำแต่ละคำจากประโยคปัจจุบันใน vec[j] และเก็บใน str
  • ทำเครื่องหมายคำเหล่านี้เป็น true ใน map check โดยใช้ check[str]=true
  • ทำซ้ำสำหรับคำทั้งหมดในประโยคปัจจุบันใน vec[j]
  • ใช้ลูป for เดินทางผ่าน vector set และสำหรับประโยคปัจจุบัน หาว่าคำเหล่านี้ใน check ก็อยู่ใน set ด้วย
  • ใช้ลูป for เดินทางผ่าน vector set อีกครั้ง
  • ถ้าคำปัจจุบันปรากฏในทุกประโยค set[k].second จะเป็น true ถ้าใช่ เพิ่ม count
  • ที่สิ้นสุด เราจะมีตัวแปร count กับคำที่เกิดขึ้นในทุกประโยค
  • คืนค่า count เป็นผลลัพธ์

ตัวอย่างโค้ด

#include <bits/stdc++.h> 
using namespace std; 

int words_sentences(vector<string> vec, int size){ 
    int count = 0; 
    int i = 0; 
    string str; 
    unordered_map<string, bool> check; 
    vector<pair<string, bool>> set; 
    pair<string, bool> str_bool; 
    while (i < vec[0].size()){ 
        str = ""; 
        while (i < vec[0].size() && vec[0][i] != ' '){ 
            str += vec[0][i]; 
            i++; 
        } 
        i++; 
        if (str != ""){ 
            str_bool = make_pair(str, true); 
            set.push_back(str_bool); 
        } 
    } 
    for (int j = 1; j < size; j++){ 
        check.clear(); 
        i = 0; 
        while (i < vec[j].size()){ 
            str = ""; 
            while (i < vec[j].size() && vec[j][i] != ' '){ 
                str += vec[j][i]; 
                i++; 
            } 
            i++; 
            if (str != ""){ 
                check[str] = true; 
            } 
        } 
        for(int k = 0; k < set.size(); k++){ 
            if (set[k].second != false && check[set[k].first] == false){ 
                set[k].second = false; 
            } 
            else if (set[k].second != false && check[set[k].first] == true){ 
                check[set[k].first] = false; 
            } 
        } 
    } 
    for (int k = 0; k < set.size(); k++){ 
        if (set[k].second == true){ 
            count++; 
        } 
    } 
    return count; 
} 

int main(){ 
    vector<string> vec; 
    vec.push_back("Honesty is the best policy"); 
    vec.push_back("policy varies from company to company"); 
    vec.push_back("Employee should follow the policy of a company"); 
    int size = vec.size(); 
    cout << "Count of words that are present in all the given sentences are: " << words_sentences(vec, size); 
    return 0; 
}

เอาต์พุต

Count of words that are present in all the given sentences are: 1