标签:cond values pre example sed bit memset hid port
Given a string containing only alpha-numerals (0-9,
A-Z and a-z) you have to count the frequency (the
number of times the character is present) of all the
characters and report only those characters whose fre-
quency is a prime number. A prime number is a num-
ber, which is divisible by exactly two different integers.
Some examples of prime numbers are 2, 3, 5, 7, 11 etc.
Input
The rst line of the input is an integer T (0 < T < 201)
that indicates how many sets of inputs are there. Each
of the next T lines contains a single set of input.
The input of each test set is a string consisting
alpha-numerals only. The length of this string is positive and less than 2001.
Output
For each set of input produce one line of output. This line contains the serial of output followed by the
characters whose frequency in the input string is a prime number. These characters are to be sorted in
lexicographically ascending order. Here \lexicographically ascending" means ascending in terms of the
ASCII values. Look at the output for sample input for details. If none of the character frequency is a
prime number, you should print `empty‘ (without the quotes) instead.
Sample Input
3
ABCC
AABBBBDDDDD
ABCDFFFF
Sample Output
Case 1: C
Case 2: AD
Case 3: empty
题意:输入T表示样例个数,接下来T行每行输入一个字符串(含大小写字母,数字),然后记录每一个字符的出现个数,最后把出现个数为素数的字符按ASCII码值由小到大排序。
分析:用map和素数筛
AC code:
#include<bits/stdc++.h> using namespace std; char s[2005]; bool u[2005]; char ans[2005]; map<char,int> book; map<char,int>::iterator it; void ass() { memset(u,true,sizeof(u)); u[0]=u[1]=false; for(int i=2;i<=2100;i++) { if(u[i]) { for(int j=2;j<=2100;j++) { if(i*j>2100) break; u[i*j]=false; } } } } int main() { //freopen("input.txt","r",stdin); int t; ass(); scanf("%d",&t); int k=0; while(t--) { scanf("%s",s); int len=strlen(s); for(int i=0;i<len;i++) { book[s[i]]++; } int num=0; for(it=book.begin();it!=book.end();it++) { if(u[it->second]) { ans[num++]=it->first; } } if(num!=0) { sort(ans,ans+num); printf("Case %d: ",++k); for(int i=0;i<num;i++) { printf("%c",ans[i]); } printf("\n"); } else printf("Case %d: empty\n",++k); book.clear(); } return 0; }
标签:cond values pre example sed bit memset hid port
原文地址:https://www.cnblogs.com/cautx/p/11427605.html