用整数替换字符串中重复的单词

发布于 2024-07-23 07:30:50 字数 250 浏览 5 评论 0原文

我在使用 C++ 进行字符串操作时遇到问题。

规则:如果句子或段落中重复出现相同的“单词”,我希望它成为一个整数。

示例:

  • 输入:我们更喜欢可以回答的问题,而不仅仅是我们讨论过的问题。
  • 输出:1 更喜欢可以回答问题 2,而不仅仅是讨论了 1 个问题 2。
1 we
2 that

I have problem in string maniputation with C++.

The Rule: if the same 'word' is repeated from sentences or paragraph I want it to become an integer.

Example:

  • input: we prefer questions that can be answered, not just we discussed that.
  • output: 1 prefer questions 2 can be answered, not just 1 discussed 2.
1 we
2 that

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

失而复得 2024-07-30 07:30:50

这是我会采取的方法(仅算法,因为它是家庭作业)。

  1. 创建一个将单词映射到计数的数据结构。
  2. 一次一个字地处理文本。
    • 如果它是一个新单词,请将其添加到数据结构中并将其计数设置为 1。
    • 如果是现有的,只需增加计数即可。
  3. 处理完所有单词后,遍历数据结构中的每个单词,为计数大于 1 的单词提供唯一的整数。
  4. 创建一个新的文本字符串,一开始为空,然后再次逐字处理文本。
    • 如果该单词的计数为 1,则将该单词附加到新字符串中。
    • 如果计数大于 1,请附加唯一整数。

This is the approach I would take (algorithms only, since it's homework).

  1. Create a data structure mapping words to counts.
  2. Process the text one word at a time.
    • if it's a new word, add it to the data structure and set its count to 1.
    • if it's an existing one, just increment the count.
  3. Once all words are processed, go through each word in the data structure, giving a unique integer to those with a count greater than one.
  4. Create a new text string, empty to start with then process the text word-by-word again.
    • if the word has a count of one, append that word to the new string.
    • if the count is greater than one, append the unique integer.
怪我太投入 2024-07-30 07:30:50

如果您使用关联数组来跟踪您已经看到的单词,这种类型的问题通常更容易解决。 尝试使用 STL 映射 来存储您已经见过的单词。 正确设置你的逻辑需要一些工作,但是地图肯定会对你想要做的事情有所帮助。

This type of problem is usually much easier to solve if you use an associative array to keep track of the words you have already seen. Try using an STL map for storing words you have seen already. It will take some work to get your logic set up correctly, but a map will definitely help with what you are trying to do.

输什么也不输骨气 2024-07-30 07:30:50

解析:

   For each word in the string
          Check whether the word exists in map<WORD,Counter>
          if the WORD is new the insert into the map with counter =0
          otherwise increment the counter associated with word.

输出:(创建新句子)

For each word in the string
      Lookup into the vector for counter value
      if counter ==0 then insert WORD as it is
      otherwise convert the counter to string and insert 

Parsing:

   For each word in the string
          Check whether the word exists in map<WORD,Counter>
          if the WORD is new the insert into the map with counter =0
          otherwise increment the counter associated with word.

Output:(create new sentence)

For each word in the string
      Lookup into the vector for counter value
      if counter ==0 then insert WORD as it is
      otherwise convert the counter to string and insert 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文