prime frequency java_UVA10789 Prime Frequency【筛选法】

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 frequency is a prime number. A prime number is a number, which is divisible by exactly two different integers. Some examples of prime numbers are 2, 3, 5, 7, 11 etc.

98f307f1ee92bbdbed04e005538fdfa1.png

Input

The first 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

问题链接:UVA10789 Prime Frequency

问题简述:(略)

问题分析:

统计给定的字符串,该字符串中只包含(0-9)、(A-Z)和(a-z),输出个数为素数的字符,如果没有个数为素数的字符则输出empty。

素数打表是必要的,可以提高计算速度。

程序说明:(略)

参考链接:(略)

题记:(略)

AC的C++语言程序如下:

/* UVA10789 Prime Frequency */

#include

using namespace std;

const int N = 2000;

const int SQRTN = sqrt((double) N);

bool prime[N + 1];

// Eratosthenes筛选法

void esieve(void)

{

memset(prime, true, sizeof(prime));

prime[0] = prime[1] = false;

for(int i = 2; i <= SQRTN; i++) {

if(prime[i]) {

for(int j = i * i; j <= N; j += i) //筛选

prime[j] = false;

}

}

}

const int L = 2001;

char s[L];

const int K = 128;

int ccnt[K];

int main()

{

esieve();

int t, caseno = 0;

scanf("%d", &t);

while(t--) {

memset(ccnt, 0, sizeof(ccnt));

scanf("%s", s);

for(int i = 0; s[i]; i++)

ccnt[(int)s[i]]++;

printf("Case %d: ", ++caseno);

int cnt = 0;

for(int i = 0; i < K; i++)

if(prime[ccnt[i]]) {

cnt++;

printf("%c", (char)i);

}

if(cnt == 0) printf("empty");

printf("\n");

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值