码迷,mamicode.com
首页 > 其他好文 > 详细

UVA10789 Prime Frequency【筛选法】

时间:2019-02-12 18:45:36      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:c++   size   class   net   out   serial   ogr   single   筛选   

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.
技术图片
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 <bits/stdc++.h>

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;
}

UVA10789 Prime Frequency【筛选法】

标签:c++   size   class   net   out   serial   ogr   single   筛选   

原文地址:https://www.cnblogs.com/tigerisland45/p/10366227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!