1、读题
1800:统计字符分数: 1
时间限制:1 秒
内存限制:32 兆
特殊判题: 否
提交:11
解决: 9
标签
字符串处理统计字符
题目描述
统计一个给定字符串中指定的字符出现的次数。
输入格式
测试输入包含若干测试用例,每个测试用例包含2行,第1行为一个长度不超过5的字符串,第2行为一个长度不超过80的字符串。注意这里的字符串包含空格,即空格也可能是要求被统计的字符之一。当读到'#'时输入结束,相应的结果不要输出。
输出
对每个测试用例,统计第1行中字符串的每个字符在第2行字符串中出现的次数,按如下格式输出:c0 n0c1 n1c2 n2... 其中ci是第1行中第i个字符,ni是ci出现的次数。
样例输入
ITHIS IS A TESTi ngthis is a long test string#
样例输出
I 2i 3 5n 2g 2
提示[+]
*** 提示已隐藏,点击上方 [+] 可显示 ***
分类
浙江大学研究生复试上机真题
2、算法实现
#include <iostream>
#include <string>
using namespace std;
int main()
{
string dest;
string source;
int len1,len2,i,j;
while(true){
getline(cin,dest);
if(dest == "#") break;
getline(cin,source);
//cout<<dest<<" "<<source<<endl;
len1 = dest.length();
len2 = source.length();
//cout<<len1<<" "<<len2<<endl;
for(i=0;i<len1;++i){
int cnt = 0;
cout<<dest[i]<<" ";
for(j=0;j<len2;++j){
if(source[j]==dest[i])++cnt;
}
cout<<cnt<<endl;
}
}
return 0;
}
3、提交代码
提交代码查看结果