题解 | #统计字符#
统计字符
https://www.nowcoder.com/practice/4ec4325634634193a7cd6798037697a8
#include<bits/stdc++.h> #include <unordered_map> using namespace std; //统计字符串中指定字符出现的次数 int count(char c, string str2) { int sum = 0; for (char s : str2) { if (s == c) { sum++; } } return sum; } int main() { string str1, str2; while (getline(cin, str1)) { //str1为"#"时循环结束 if (str1 == "#") { break; } else { getline(cin, str2); //遍历字符串1,统计字符串2中某字符出现的次数并输出 for (char c :str1) { cout << c << " " << count(c, str2) << endl; } } } }