UVA10789 Prime Frequency筛选法

Posted tigerisland45

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.
技术图片
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("
");
    }

    return 0;
}

以上是关于UVA10789 Prime Frequency筛选法的主要内容,如果未能解决你的问题,请参考以下文章

UVA10780 Again Prime? No Time.

线性筛

线性筛素数(欧拉筛)

欧拉筛素数+求欧拉函数

模板欧拉筛

欧几里德筛法