我在使用 C++ 进行字符串操作时遇到问题。
规则:如果句子或段落中重复相同的“单词”,我希望它变成一个整数。
例子:
- 输入:
we prefer questions that can be answered, not just we discussed that.
- 输出:
1 prefer questions 2 can be answered, not just 1 discussed 2.
1 we
2 that
我在使用 C++ 进行字符串操作时遇到问题。
规则:如果句子或段落中重复相同的“单词”,我希望它变成一个整数。
例子:
we prefer questions that can be answered, not just we discussed that.
1 prefer questions 2 can be answered, not just 1 discussed 2.
1 we
2 that
如果您使用关联数组来跟踪您已经看过的单词,这种类型的问题通常更容易解决。尝试使用STL 映射来存储您已经看过的单词。正确设置逻辑需要一些工作,但地图肯定会帮助您尝试做的事情。
这是我将采取的方法(仅限算法,因为它是家庭作业)。
解析:
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