D. 字符计数(指针)

38 阅读1分钟
D. 字符计数(指针)
题目描述

统计一段字符中不同字符出现的频率

注意:所有统计操作必须使用指针完成!


输入

第一行为测试数据组数,每组测试数据为一长字符串(保证输入数据中只含有大小写字母和数字),最大长度为10000


输出

输出每组数据中按照字符升序后的各个字符的出现频率

 
输入样例1 
2
WoAiDadaima
ILOVESHENZHENUniversity2021
输出样例1
A:1 D:1 W:1 a:3 d:1 i:2 m:1 o:1 
0:1 1:1 2:2 E:3 H:2 I:1 L:1 N:2 O:1 S:1 U:1 V:1 Z:1 e:1 i:2 n:1 r:1 s:1 t:1 v:1 y:1 

该题考察对指针的使用,比较简单,下面看代码

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include <iomanip>
#include<cmath>
#include<cstring>
#include<cctype>
#include<queue>
using namespace std;//使用常量数组作为排序依据
char sheet[100] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const int MAXN = 10003;
char str[MAXN];
int main()
{
	int t,cn=0;
	char* p = sheet;
	char* p2 = str;
	cin >> t;
	while (t--)
	{
		cin >> str;
		int len = strlen(str);
		for (int i = 0; i < strlen(sheet); i++)
		{
			cn = 0;
			for (int j=0;j<len; j++)
			{
				if (*(p2+j) == *(p+i))
				{
					cn++;
				}
			}
			if (cn == 0)
				continue;
			cout << (*(p + i)) << ":" << cn << ' ';
		}
		cout << endl;
	}
	return 0;
}