题解 | #单词识别#

单词识别

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;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-10 14:10
啊啊啊啊好幸福,妈妈是我找工作发疯前的一束光
榕城小榕树:你是我见过最幸福的牛客男孩
点赞 评论 收藏
分享
认真搞学习:这么良心的老板真少见
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务