题解 | #单词识别#
单词识别
https://www.nowcoder.com/practice/16f59b169d904f8898d70d81d4a140a0
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
string s;
int main()
{
getline(cin, s);
map<string, int> m;
// 分割每个单词 并 存在map中
size_t left = 0, right = s.find(" ");
while (right != string::npos)
{
string tmp(s, left, right - left);
transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
m[tmp]++;
left = right + 1;
right = s.find(" ", left);
}
// 因为最后一个单词无法查找所以得做处理
string tmp(s, left, s.size() - left - 1);
m[tmp]++;
// 打印
for (const auto& e : m)
{
cout << e.first << ":" << e.second << endl;
}
return 0;
}
三奇智元机器人科技有限公司公司福利 80人发布
查看3道真题和解析