首页 > 试题广场 >

统计字符

[编程题]统计字符
  • 热度指数:18720 时间限制: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
#include<stdio.h>
#include<string.h>

int main()
{
    char str1[5], str2[80];
    
    while (1) 
    {
        int i = 0;
        int count[128] = { 0 };

        while ((str1[i] = getchar()) != '\n' && str1[i] != '#' && (str1[i])!=EOF)
            i++;
        if (str1[i] == '#'|| str1[i]== EOF)
            break;
        str1[i] = '\0';
        gets(str2);
        for (i = 0 ; i <strlen(str2); i++)
            count[str2[i]]++;
        for (i = 0 ; i < strlen(str1); i++)
            printf("%c %d\n", str1[i],count[str1[i]]);
    }
    return 0;
}
一直提示段错误,突然想到没有判断文件是否结束。
编辑于 2020-03-19 22:15:57 回复(0)
#include <iostream>
using namespace std;
int main()
{
    string s1,s2;
    while(getline(cin,s1)){
        if(s1=="#") break;
        getline(cin,s2);
        for(int i=0;i<s1.size();i++){
            int num=0;
            for(int j=0;j<s2.size();j++){
                if(s1[i]==s2[j]){
                    num++;
                }
            }
        cout<<s1[i]<<" "<<num<<endl;
        }
    }
    return 0;
}

发表于 2019-06-17 15:39:41 回复(0)
count的那个方法真的厉害,小菜鸡就不卖弄了
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main(){
    string str1,str2;
    while(getline(cin,str1)){
        if(str1=="#")
            break;
        getline(cin,str2);
        char ch1[5],ch2[80];
        strcpy(ch1,str1.c_str());
        strcpy(ch2,str2.c_str());
        int len1=strlen(ch1),len2=strlen(ch2);
        int *count=new int[len1];
        for(int i=0;i<len1;i++)
            count[i]=0;
        for(int i=0;i<len2;i++){
            for(int j=0;j<len1;j++){
                if(ch2[i]==ch1[j]){
                    count[j]++;
                }
            }
        }
        for(int i=0;i<len2;i++){
            cout<<ch1[i]<<' '<<count[i]<<endl;
        }
    }
}

发表于 2019-03-02 16:07:34 回复(0)
while True:
    try:
        string1 = input()
        if string1 == '#':
            break
        string2 = input()
        for i in string1:
            print("%s %d" % (i,string2.count(i)))
    except Exception:
        break
编辑于 2018-09-25 14:50:24 回复(0)

看到模板,HASH打表法都不好意思了

#include
#include
#include
using namespace std;
const int maxn = 1e5 + 5;
int hashT[maxn];
char s1[10];
char s2[85];
int main() {
  int len1, len2;
  while(gets(s1) && strcmp(s1, "#") != 0) {
    memset(hashT, 0, sizeof(hashT));
    gets(s2);
    len1 = strlen(s1);
    len2 = strlen(s2);
    for(int i = 0; i < len2; i++) {
      hashT[s2[i]]++;
    }
    for(int i = 0; i < len1; i++) {
      printf("%c %d\n", s1[i], hashT[s1[i]]);
    }
  }
  return 0;
}
发表于 2018-04-15 21:17:11 回复(0)

python解法,应该是最简单了的吧

while True:
    try:
        a,b=input(),input()
        for i in a:
            print(i+" "+str(b.count(i)))
    except:
        break
编辑于 2017-10-06 21:24:09 回复(0)
//c++ STL了解一下
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
    string a,b;
    while(getline(cin,a)&&a!="#")
    {
        getline(cin,b);
        for(int i=0;i<a.size();i++)
        {
            cout<<a[i]<<" "<<count(b.begin(),b.end(),a[i])<<endl; 
        }
    }
    return 0;
}

发表于 2018-03-18 10:27:37 回复(4)
#include <stdio.h>
#include <string.h>
#define N 81

int main()
{
    char s1[N], s2[N];
    int len1, len2;
    int count;
    while(gets(s1))
    {
        if(s1[0]=='#') break;
        gets(s2);
        len1=strlen(s1);
        len2=strlen(s2);
        for(int i=0; i<len1; i++)
        {
            count=0;
            for(int j=0; j<len2; j++)
            {
                if(s1[i]==s2[j]) count++;
            }
            printf("%c %d\n", s1[i], count);
        }
    }
    return 0;
}

发表于 2018-02-07 08:44:35 回复(0)
经典哈希思想
发表于 2020-06-09 10:36:50 回复(0)
#include<stdio.h>
int main()
{
    char a[10],b[100],i,j,num;
    gets(a);
    gets(b);
    for(i=0;a[i]!='\0';i++)
    {
        num=0;
        for(j=0;b[j]!='\0';j++)
            if(a[i]==b[j])
                num++;
        printf("%c %d\n",a[i],num);
    }
}

发表于 2020-04-14 16:21:44 回复(0)
//本代码思路:创建一个ascii整型数组,收集第二行字符串中每个字符出现的次数
//然后按第一行字符串的需要打印次数
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int[] ascii = new int[127];
            char[] target = sc.nextLine().toCharArray();
            char[] str = sc.nextLine().toCharArray();
            for(int i=0;i<str.length;i++){
                ascii[(int)(str[i])-1]++;
            }
            for(int i=0;i<target.length;i++){
                char c = (char)(target[i]);
                System.out.println(c+" "+ascii[(int)(target[i])-1]);
            }
        }
    }
}

编辑于 2018-10-18 20:10:07 回复(0)
#include <cstring>
#include <cstdio>

using namespace std;

int main()
{
    //freopen("date.txt", "r", stdin);
    char str1[6], str2[81];
    
    while(gets(str1) && str1[0] != '#'){
        gets(str2);
        int count[128] = {0};//统计128个ASCII出现的次数,每次统计都要清零
        for(int i = 0; i < strlen(str2); i++)//统计str2中每个字符出现的次数,只需遍历一遍
            count[(int) str2[i]]++;

        for(int i = 0; i < strlen(str1); i++)
            printf("%c %d\n", str1[i], count[(int) str1[i]]);
    }

    return 0;
}

发表于 2018-02-13 09:56:27 回复(0)
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string strp, stra;
    while (getline(cin, strp) && "#" != strp)
    {
        getline(cin, stra);
        int Count[128] = { 0 };
        for (int i = 0; i < stra.size(); ++i)
        {
            ++Count[stra[i]];
        }

        for (int i = 0; i < strp.size(); ++i)
        {
            cout << strp[i] << ' ' << Count[strp[i]] << endl;
        }
    }
    return 0;
}
发表于 2020-02-19 14:09:05 回复(0)
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

const int MAXN = 5;
struct Count{
	char c;
	int n;
};

//统计字符 
int main(){
	string str, str2;
	while (getline(cin, str)) {
		if (str == "#") {
			break;
		}
		int num;
		Count s[MAXN];
		
		//将所有s.n 赋初值0 
		for (int i = 0; i < str.length(); i++){
			s[i].c = str[i];
			s[i].n = 0;
		}
		
		//统计要记录的字符个数 
		num = str.length();
		
		//获取要统计的字符串 
		getline(cin, str2);
		
		for (int i = 0; i < str2.length(); i++){
			for (int j = 0; j < num; j++){
				if (s[j].c == str2[i]) {
					s[j].n++;
				}
			}
		}
		
		for (int i = 0; i < num; i++){
			cout << s[i].c << " " << s[i].n << endl;
		}
	}
	return 0;
} 

发表于 2023-03-22 16:41:23 回复(0)
#include <string>
#include <iostream>
using namespace std;
int main() {
	char a[5];
	char b[80];
	while (cin.getline(a,5)&&a!="#") {
		cin.getline(b, 80);
		for (int i = 0; a[i] != '\0'; i++) {
			int count = 0;
			for (int j = 0; b[j] != '\0'; j++) {
				if (a[i] == b[j]) {
					count++;
				}
			}
			cout << a[i] << ' ' << count << endl;
		}
	}
	return 0;
}

发表于 2023-02-05 10:16:06 回复(0)
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main() {
	string str1;
	string str2;
	getline(cin, str1);
	getline(cin, str2);
	int sum = 0;
	for (int i = 0; i < str1.size(); i++) {
		cout << str1[i]<<" ";
		for (int j = 0; j < str2.size(); j++) {
			if (str1[i] == str2[j])
				sum++;
		}
		cout << sum << endl;
        sum = 0;
	}

	return 0;
}

发表于 2023-01-30 18:10:36 回复(0)
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;


int main(){
    string s1,s2;
    while(getline(cin,s1)&&s1!="#"){
        //cin.get();
        getline(cin,s2);
        for(int i=0;i<s1.length();i++){
            int k=0;
            for(int j=0;j<s2.length();j++){
                if(s2[j]==s1[i])
                k++;
            }
        cout<<s1[i]<<' '<<k<<endl;
        }
    }
}

发表于 2019-03-13 21:26:15 回复(0)
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main() {
    string a,b;
    while(getline(cin,a)){
        if(a=="#") break;
        getline(cin,b);
        for(int i=0;i<a.size();i++){
            int cnt=0;
            for(int j=0;j<b.size();j++)
                if(a[i]==b[j]) cnt++;
            cout<<a[i]<<' '<<cnt<<endl;
        }
    }
}
发表于 2024-03-21 15:25:30 回复(0)
#include <bits/stdc++.h>
using namespace std;

int main()
{
    string s1,s2;
    while(getline(cin,s1))
    {
        map<char,int> myMap;
        if(s1=="#")    break;
        getline(cin,s2);
        for(int i=0;i<s1.size();i++)
        {
            if(myMap[s1[i]]) continue;
            for(int j=0;j<s2.size();j++)
            {
                if(s1[i]==s2[j])    myMap[s1[i]]++;
            }
        }
        for(int i=0;i<s1.size();i++)
        {
            cout<<s1[i]<<" "<<myMap[s1[i]]<<endl;
        }
    }
    return 0;
}
发表于 2024-03-17 17:15:16 回复(0)
#include<iostream>
#include<vector>
#include<string>
#include<set>
using namespace std;
void countTimes(string a,string b){
     multiset<char> str;
     for(int i=0;i<b.size();i++){
        str.insert(b[i]);
     }
     for(int i=0;i<a.size();i++){
        printf("%c %d\n",a[i],str.count(a[i]));
     }
}
int main(){
   vector<string> vec1; //用于存储需要匹配的字符序列
   vector<string> vec2; //用于存储待匹配的主串

   while(1){
      string str1,str2;
      getline(cin,str1);
      if(str1.front()=='#'||str1.empty()) break;
      vec1.push_back(str1);
      getline(cin,str2);
      vec2.push_back(str2);
   }
   for(int i = 0;i < vec1.size();i++){
      countTimes(vec1[i], vec2[i]);
   }
}

编辑于 2024-03-11 16:34:38 回复(0)