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

UVA 10789 题解

时间:2019-08-29 09:25:09      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:cond   values   pre   example   sed   bit   memset   hid   port   

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

 

UVA 10789 题解

标签:cond   values   pre   example   sed   bit   memset   hid   port   

原文地址:https://www.cnblogs.com/cautx/p/11427605.html

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