C++ 打印两个给定句子中所有不重复的单词

C++ 打印两个给定句子中所有不重复的单词

在本教程中,我们将识别并打印出两个给定句子中所有不重复的单词。不重复的单词指的是在两个句子中只出现一次的单词,即它们在另一个句子中不重复出现。该任务包括分析输入句子,识别单词,并在两个句子中比较它们,找出只出现一次的单词。输出应该是所有这些单词的列表。这个任务可以通过各种编程方法来完成,比如使用循环、数组或字典。

方法

下面是两种从两个给定句子打印所有不重复单词的方法−

方法1:使用字典

方法2:使用集合

方法1:使用字典

使用字典,统计两个短语中每个单词出现的次数。然后我们可以遍历字典并打印出只出现一次的所有单词。在C++中,字典函数通常用于输出两个指定句子中的所有不重复的单词。该方法包括使用字典或哈希表数据结构存储两个短语中每个单词的频率。然后我们可以迭代地遍历字典并打印出只出现一次的术语。

语法

这里是使用字典方法在C++中打印两个给定句子中所有不重复单词的语法,不包含实际代码−

  • 声明一个字典来存储单词频率
map<string, int> freqDict;
  • 输入两个句子字符串
string sentence1 = "first sentence";
string sentence2 = "second sentence";
  • 将句子拆分为单词并插入词典中
istringstream iss (sentence1 + " " + sentence2);
string word;
while (iss >> word) {
   freqDict[word]++;
}
  • 迭代字典并打印出不重复的单词
for (const auto& [word, frequency]: freqDict) {
   if (frequency == 1) {
      cout << word << " ";
   }
}

步骤

在C ++中,以下是使用字典方法打印两个指定句子中所有非重复词的逐步技术:

步骤1 - 创建两个包含句子的字符串s1和s2。

步骤2 - 声明空的无序映射string, int> dict,以记录句子中每个词的频率。

步骤3 - 使用C ++的字符串流类解析两个短语以提取单个单词。

步骤4 - 对于每个提取的单词,检查它是否出现在dict中。如果是,则增加其频率1。否则,将其添加到具有频率1的dict中。

步骤5 - 处理两个句子后,遍历dict并显示所有频率为1的项。这些是两个句子中的非重复词。

步骤6 - 这种方法的时间复杂性为O(n)。

示例1

此代码使用无序映射存储组合短语中每个单词的频率。然后,它遍历映射,将每个只出现一次的单词添加到非重复单词的向量中。最后,它发布非重复单词。该示例暗示两个句子被硬编码到程序中,而不是由用户输入。

#include <iostream>
#include <string>
#include <unordered_map>
#include <sstream>
#include <vector>

using namespace std;

vector<string> getNonRepeatingWords(string sentence1, string sentence2) {
   // Combine the two sentences into a single string
   string combined = sentence1 + " " + sentence2;

   // Create a map to store the frequency of each word
   unordered_map<string, int> wordFreq;

   // Use a string stream to extract each word from the combined string
   stringstream ss(combined);
   string word;
   while (ss >> word) {
      // Increment the frequency of the word in the map
      wordFreq[word]++;
   }

   // Create a vector to store the non-repeating words
   vector<string> nonRepeatingWords;
   for (auto& pair : wordFreq) {
      if (pair.second == 1) {
         nonRepeatingWords.push_back(pair.first);
      }
   }

   return nonRepeatingWords;
}
int main() {
   string sentence1 = "The quick brown fox jumps over the lazy dog";
   string sentence2 = "A quick brown dog jumps over a lazy fox";

   vector<string> nonRepeatingWords = getNonRepeatingWords(sentence1, sentence2);

   // Print the non-repeating words
   for (auto& word : nonRepeatingWords) {
      cout << word << " ";
   }
   cout << endl;

   return 0;
}

输出

a A the The

方法2:使用集合

该策略使用集合来定位只在两个短语中出现一次的术语。我们可以为每个短语构建术语集合,然后识别这些集合的交集。最后,我们可以迭代遍历交集集合,并输出只出现一次的所有术语。

集合是一种关联容器,以排序顺序保存不重复的元素。我们可以将来自两个短语的术语插入到集合中,任何重复项都会被自动删除。

语法

当然!以下是您可以在Python中使用的语法,用于打印出两个给定句子中所有不重复的单词 −

  • 将两个句子定义为字符串
sentence1 = "The fox jumps over dog"
sentence2 = "A dog jumps over fox"
  • 将每个句子拆分成单词列表
words1 = sentence1.split()
words2 = sentence2.split()
  • 从这两个单词列表中创建集合
set1 = set(words1)
set2 = set(words2)
  • 通过求集合交集来查找不重复的单词
Nonrepeating = set1.symmetric_difference(set2)
  • 打印不重复的单词
for word in non-repeating:
   print(word)

步骤

请按照以下步骤使用C++中的集合函数输出两个给定句子中的所有不重复单词:

步骤1 − 创建两个字符串变量来存储两个句子。

步骤2 − 使用字符串流库将每个句子拆分为独立的单词,并将它们存储在两个单独的数组中。

步骤3 − 创建两个集合,一个用于每个句子,来存储唯一的单词。

步骤4 − 循环遍历每个单词数组,并将每个单词插入到正确的集合中。

步骤5 − 遍历每个集合,打印出不重复的单词。

示例2

在这段代码中,我们使用字符串流库将每个句子拆分为单独的单词。然后我们使用两个集合uniqueWords1和uniqueWords2来存储每个句子中的唯一单词。最后,我们循环遍历每个集合并打印出不重复的单词。

#include <iostream>
#include <string>
#include <sstream>
#include <set>

using namespace std;

int main() {
   string sentence1 = "This is the first sentence.";
   string sentence2 = "This is the second sentence.";
   string word;
   stringstream ss1(sentence1);
   stringstream ss2(sentence2);
   set<string> uniqueWords1;
   set<string> uniqueWords2;

   while (ss1 >> word) {
      uniqueWords1.insert(word);
   }

   while (ss2 >> word) {
      uniqueWords2.insert(word);
   }

   cout << "Non-repeating words in sentence 1:" << endl;
   for (const auto& w : uniqueWords1) {
      if (uniqueWords2.find(w) == uniqueWords2.end()) {
         cout << w << " ";
      }
   }
   cout << endl;

   cout << "Non-repeating words in sentence 2:" << endl;
   for (const auto& w : uniqueWords2) {
      if (uniqueWords1.find(w) == uniqueWords1.end()) {
         cout << w << " ";
      }
   }
   cout << endl;

   return 0;
}

输出

Non-repeating words in sentence 1:
first 
Non-repeating words in sentence 2:
second

结论

总之,使用各种编程方法(例如将句子分解为单词、使用字典来量化每个单词的频率,并过滤出不重复的单词),已实现从两个提供的句子中打印出所有非重复单词的任务。得到的非重复单词集合可以报告给控制台,也可以保存在列表或数组中以供进一步使用。这项工作对于基本的编程文本操作和数据结构操作是有帮助的练习。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程