题解 | #单词识别#
单词识别
https://www.nowcoder.com/practice/16f59b169d904f8898d70d81d4a140a0
map底层采用红黑树使用
for (auto it = alpha.begin(); it != alpha.end(); advance(it, 1))
可以从小到大按照字典序排序
#include <bits/stdc++.h>
using namespace std;
string to_lower(string s) {
for (char& ch : s)ch = tolower(ch);
return s;
}
int main() {
map<string, int> alpha;
string s;
while (cin >> s) {
if (s.back() == '.')s = s.substr(0, s.size() - 1);
transform(s.begin(), s.end(), s.begin(), ::tolower);
alpha[s]++;
}
for (auto it = alpha.begin(); it != alpha.end(); advance(it, 1)) {
cout << it->first << ":" << it->second << endl;
}
return 0;
}
// 64 位输出请用 printf("%lld")