首页 > 试题广场 >

统计字符

[编程题]统计字符
  • 热度指数:18850 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    统计一个给定字符串中指定的字符出现的次数。

输入描述:
    测试输入包含若干测试用例,每个测试用例包含2行,第1行为一个长度不超过5的字符串,第2行为一个长度不超过80的字符串。注意这里的字符串包含空格,即空格也可能是要求被统计的字符之一。当读到'#'时输入结束,相应的结果不要输出。


输出描述:
    对每个测试用例,统计第1行中字符串的每个字符在第2行字符串中出现的次数,按如下格式输出:
    c0 n0
    c1 n1
    c2 n2
    ... 
    其中ci是第1行中第i个字符,ni是ci出现的次数。
示例1

输入

I
THIS IS A TEST
i ng
this is a long test string
#

输出

I 2
i 3
  5
n 2
g 2
头像 渺小小螃蟹
发表于 2021-05-12 14:06:06
#include<iostream> #include<cstdio> #include<string> #include<cstring> using namespace std; int number[128]; int main() { 展开全文
头像 KoukiAlpha
发表于 2023-01-26 15:27:08
#include <iostream> using namespace std; void count(string s1,string s2) { int cnt; for(int i = 0;i < s1.length(); i++) { cn 展开全文
头像 coder_bai
发表于 2023-01-03 19:04:18
#include <bits/stdc++.h> using namespace std; int number[128]; int main() { string str1, str2; while (getline(cin, str1)) { if 展开全文
头像 L456
发表于 2024-03-17 17:03:50
#include <bits/stdc++.h> using namespace std; int main() { string s1,s2; while(getline(cin,s1)) { map<char,int> myMap; if(s1==&quo 展开全文
头像 阿尔芒a
发表于 2024-03-20 19:00:28
#include<iostream> #include<string> #include<algorithm> using namespace std; int C_time(string str,char pattern) { int pos = 0 展开全文
头像 牛客434473393号
发表于 2023-03-02 12:22:48
#include <iostream> #include <string> #include <cstdio> using namespace std; int main() { string a, b; while (getline(cin, 展开全文
头像 树新峰
发表于 2023-03-30 17:44:01
#include <stdlib.h> #include <stdio.h> #include <string.h> #define len 2000 int main(){ char str[len]; char str1[len]; 展开全文
头像 牛客652687585号
发表于 2022-02-13 22:21:16
#include<iostream> #include<cstdio> #include<string> using namespace std; int count[128]; int main(){  &nbs 展开全文
头像 JXH001
发表于 2024-02-16 17:54:18
#include<iostream> #include<string> #include<cstdio> using namespace std; int main() { string s1, s2; while (getline(cin, s1)) { 展开全文
头像 牛客7777779号
发表于 2023-02-23 10:48:36
定义string变量,想要读入空格,可以用getline函数,参数为(cin,变量) #include <iostream> using namespace std; #include <string> int main() { string str1,str2; 展开全文