Problem Link:http://139.129.36.234/problem.php?id=1207
1207: 统计字符
时间限制: 1 Sec 内存限制: 38 MB提交: 13 解决: 7
[ 提交][ 状态][ 讨论版]
题目描述
统计一个给定字符串中指定的字符出现的次数。
输入
测试输入包含若干测试用例,每个测试用例包含2行,第1行为一个长度不超过5的字符串,第2行为一个长度不超过80的字符串。注意这里的字符串包含空格,即空格也可能是要求被统计的字符之一。当读到'#'时输入结束,相应的结果不要输出。
输出
对每个测试用例,统计第1行中字符串的每个字符在第2行字符串中出现的次数,按如下格式输出:
c0 n0
c1 n1
c2 n2
...
其中ci是第1行中第i个字符,ni是ci出现的次数。
c0 n0
c1 n1
c2 n2
...
其中ci是第1行中第i个字符,ni是ci出现的次数。
样例输入
I
THIS IS A TEST
i ng
this is a long test string
#
样例输出
I 2
i 3
5
n 2
g 2
提示
来源
AC code:
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<map>
#include<math.h>
#include<string.h>
#include<queue>
#include<vector>
#include<set>
#define LL long long
#define exp 1e-9
#define MAXN 1000010
using namespace std;
int main()
{
// freopen("D:\\in.txt","r",stdin);
char s1[6],s2[88];
int len1,len2,i,j,sum;
while(gets(s1)&&s1[0]!='#')
{
gets(s2);
len1=strlen(s1);
len2=strlen(s2);
for(i=0;i<len1;i++)
{
sum=0;
for(j=0;j<len2;j++)
{
if(s1[i]==s2[j])
{
sum++;
}
}
printf("%c %d\n",s1[i],sum);
}
}
return 0;
}