题解 | #统计字符#
统计字符
https://www.nowcoder.com/practice/4ec4325634634193a7cd6798037697a8
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int C_time(string str,char pattern)
{
int pos = 0;
int count = 0;
pos = str.find(pattern, pos);
while (pos != str.npos)
{
count++;
pos = str.find(pattern, pos+1);
}
return count;
}
int main()
{
string str1, str2;
while (getline(cin,str1))
{
if (str1 == "#")
{
break;
}
getline(cin, str2);
for (int i = 0; i < str1.size(); i++)
{
int times = C_time(str2, str1[i]);
printf("%c %d\n",str1[i],times);
}
}
}
查看6道真题和解析
