卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章20369本站已运行347

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

<?xml encoding="utf-8" ?>

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

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

方法

这里有两种方法来打印出两个给定句子中所有不重复的单词−

方法1:使用字典

方法2:使用集合

方法一:使用字典

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

语法

这里是没有实际代码的语法,使用 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,用于记录句子中每个单词的频率。

第三步 − 使用C++的字符串流类,解析两个短语以提取单词。

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

第5步 - 处理完两个句子后,迭代字典并显示频率为1的所有术语。这些是两个句子中不重复的单词。

步骤 6 − 这种方法的时间复杂度为 O(n),

Example 1

的中文翻译为:

示例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 - 遍历每个集合并打印出不重复的单词。

Example 2

的中文翻译为:

示例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

结论

总之,从两个提供的句子中打印所有非重复单词的任务是通过使用各种编程方法来实现的,例如将句子分解为单个单词,利用字典来量化每个单词的频率,以及过滤掉非重复单词。生成的非重复单词集合可以报告给控制台或保存在列表或数组中以供进一步使用。这项工作对于基本的编程文本操作和数据结构操作很有帮助。
卓越飞翔博客
上一篇: 我们如何使用PHP脚本显示MySQL表中的所有记录?
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏