用整数替换字符串中重复的单词
我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我会采取的方法(仅算法,因为它是家庭作业)。
This is the approach I would take (algorithms only, since it's homework).
如果您使用关联数组来跟踪您已经看到的单词,这种类型的问题通常更容易解决。 尝试使用 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.