首页 > 试题广场 >

统计字符

[编程题]统计字符
  • 热度指数:20316 时间限制: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() { 展开全文
头像 52Ulpianus
发表于 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 展开全文
头像 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 展开全文
头像 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 展开全文
头像 T790T
发表于 2024-08-03 10:49:08
#include <bits/stdc++.h> using namespace std; int main() { string s,ss; while(getline(cin,s)){ if(s == "#") break; 展开全文
头像 牛客349505404号
发表于 2025-03-18 15:47:04
#include<bits/stdc++.h> using namespace std; int main() { string p,s; while(getline(cin,p)) { if(p=="#")break; getline(cin,s); 展开全文
头像 Jonas_LEE
发表于 2025-03-17 16:03:52
本题与KY67子串计算类似,都是使用while+find循环匹配子串,这个匹配完了pos++匹配下一个范围的子串pair在本题中的使用与map几乎没有太大区别,连迭代器遍历访问都一致,只是map自动按键升序排序在有些情况下反而不合适 // #include <iostream> // # 展开全文
头像 阿尔芒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]; 展开全文