OpenJ_Bailian--4071--字符串字符的统计查找

给定一个ASCII字符串,查找字符串中,出现了k次的字符。比如,字符串"This is a good day!"中,出现了2次的字符为'a','d','i','o', 's',出现了4次的字符为' '。

Input

第一行是一个正整数n(1<=n<=100),表示下面要进行查找的字符串的数量。其后n行,每行是一个字符串(这里确保字符串的结尾不是空格),和一个数字k,字符串和数字k之间由一个空格隔开。

Output

输出要求按照ASCII码从小到大的顺序输出字符,每个字符用单引号括起来,字符间用逗号隔开。

Sample Input
2
This is a good day! 2 
This is a good day! 4
Sample Output
'a','d','i','o','s'
' '

思路:

1.对于字符串的处理很耽搁时间,需要注意;

2.采用gets()函数,需要单独拿出数字k,很麻烦,可以利用atoi函数转化;用isdigit判断当前符号是字符;

3.由于是ascll编码,数据范围0~255;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
#define ll long long
const int maxa=1e6+10;
const int maxASCLL=256;
char a[maxASCLL];
int cnt[maxASCLL]; //0--255
int main(){
	int t,s,k;
	scanf("%d",&t);
	getchar();//用于缓冲,防止'0'产生输出,导致结果错误 
	while(t--){
		gets(a);
		memset(cnt,0,sizeof cnt);
		s=strlen(a)-1;//求出末尾数字与空格
		while(isdigit(a[s]))	s--;
		k=atoi(&a[s+1]); //出现了k次的字符
		a[s]='\0';
		int i=0;
		while(a[i]){cnt[(int)a[i]]++;i++;}
		bool f=0;
		for(int i=0;i<=maxASCLL;i++){
			if(cnt[i]==k){
				if(f)	printf(",");
				f=1;
				printf("'%c'",(char)i);
			}
		}
		printf("\n");
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/queque_heiya/article/details/106185428