题解 | #单词识别#
单词识别
https://www.nowcoder.com/practice/16f59b169d904f8898d70d81d4a140a0
#include <iostream> #include <string> #include <map> #include <vector> using namespace std; int main() { string str; getline(cin, str); //去掉大写 和 最后的逗号 str[0] += 32; str[str.size()-1] = ' '; size_t dest = 0; size_t cur = str.find(' '); //在vector中插入字符 vector<string> vs; while(cur < str.size()-1) { string tmp = str.substr(dest,cur-dest); dest = cur+1; cur = str.find(' ',dest); vs.push_back(tmp); } vs.push_back(str.substr(dest,cur-dest)); //把vector的字符串插入map容器中 map<string,int> mp; for(auto ch : vs) { mp[ch]++; } //打印map auto begin = mp.begin(); while(begin != mp.end()) { cout<<begin->first<<":"<<begin->second<<endl; begin++; } return 0; } // 64 位输出请用 printf("%lld")