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[];
bool u[];
char ans[];
map<char,int> book;
map<char,int>::iterator it;
void ass()
{
memset(u,true,sizeof(u));
u[]=u[]=false;
for(int i=;i<=;i++)
{
if(u[i])
{
for(int j=;j<=;j++)
{
if(i*j>) break;
u[i*j]=false;
}
}
}
}
int main()
{
//freopen("input.txt","r",stdin);
int t;
ass();
scanf("%d",&t);
int k=;
while(t--)
{
scanf("%s",s);
int len=strlen(s);
for(int i=;i<len;i++)
{
book[s[i]]++;
}
int num=;
for(it=book.begin();it!=book.end();it++)
{
if(u[it->second])
{
ans[num++]=it->first;
}
}
if(num!=)
{
sort(ans,ans+num);
printf("Case %d: ",++k);
for(int i=;i<num;i++)
{
printf("%c",ans[i]);
}
printf("\n");
}
else printf("Case %d: empty\n",++k);
book.clear();
}
return ;
}

UVA 10789 题解的更多相关文章

  1. UVA 10131题解

    第一次写动态规划的代码,整了一天,终于AC. 题目: Question 1: Is Bigger Smarter? The Problem Some people think that the big ...

  2. 位运算基础(Uva 1590,Uva 509题解)

    逻辑运算 规则 符号 与 只有1 and 1 = 1,其他均为0 & 或 只有0 or 0 = 0,其他均为1 | 非 也就是取反 ~ 异或 相异为1相同为0 ^ 同或 相同为1相异为0,c中 ...

  3. 【OI】计算分子量 Molar mass UVa 1586 题解

    题目:(由于UVa注册不了,还是用vjudge) https://vjudge.net/problem/UVA-1586 详细说明放在了注释里面.原创. 破题点在于对于一个元素的组合(元素+个数),只 ...

  4. UVa 12171 题解

    英文题面不怎么友好,大家还是自行通过紫书了解题面吧... 解题思路: 1. 面对500 ^ 3的数据范围,我们需要先用离散化解决掉爆空间的问题. 2. 由于我们要求的总体积包括内空部分的体积,我们可以 ...

  5. UVA题解三

    UVA题解三 UVA 127 题目描述:\(52\)张扑克牌排成一列,如果一张牌的花色或者数字与左边第一列的最上面的牌相同,则将这张牌移到左边第一列的最上面,如果一张牌的花色或者数字与左边第三列的最上 ...

  6. UVA题解二

    UVA题解二 UVA 110 题目描述:输出一个Pascal程序,该程序能读入不多于\(8\)个数,并输出从小到大排好序后的数.注意:该程序只能用读入语句,输出语句,if语句. solution 模仿 ...

  7. [题解]UVa 11082 Matrix Decompressing

    开始眨眼一看怎么也不像是网络流的一道题,再怎么看也觉得像是搜索.不过虽然这道题数据范围很小,但也不至于搜索也是可以随随便便就可以过的.(不过这道题应该是special judge,因为一题可以多解而且 ...

  8. [题解]UVa 10891 Game of Sum

    在游戏的任何时刻剩余的都是1 - n中的一个连续子序列.所以可以用dp[i][j]表示在第i个数到第j个数中取数,先手的玩家得到的最大的分值.因为两个人都很聪明,所以等于自己和自己下.基本上每次就都是 ...

  9. [题解]UVa 10635 Prince and Princess

    讲一下题目大意,就是有两个长度为p + 1和q + 1的序列,求它们的LCS. 如果用O(pq)的算法对于这道题来说还是太慢了.所以要另外想一些方法.注意到序列中的所有元素都不相同,所以两个序列中数对 ...

随机推荐

  1. Python - MySQL 数据库连接 - PyMySQL 驱动 - 第二十五天

    序言 本文我们为大家介绍 Python3 使用 PyMySQL 连接数据库,并实现简单的增删改查. 什么是 PyMySQL? PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务 ...

  2. Mysql 报错:#1067 - Invalid default value for 'update_time

    由于 字段UPDATE_TIME 的字段类型是 timestamp ,默认值是:'0000-00-00 00:00:00' 即:`UPDATE_TIME` timestamp NOT NULL DEF ...

  3. SAP S4HANA BP事务代码初始界面的ROLE和Grouping配置

    SAP S4HANA BP事务代码初始界面的ROLE和Grouping配置 SAP S/4 HANA系统里,创建供应商不再使用MK01/FK01/XK01等事务代码,而是使用BP事务代码. BP事务代 ...

  4. 【设计模式】Builder

    前言 Builder设计模式,允许一步一步构建一个复杂的对象.将构建步骤抽象出来,让每个具体的Builder去实现构建步骤的内容.这样子就可以用同样的构建步骤,构建出不一样的对象.在Director类 ...

  5. 【已采纳】新项目第一次怎么上传到github里面

      言归正传,最近学习了怎么将新创建的本地代码上传到github上,这里简单的记录一下,我喜欢使用命令行,这里全用命令行来实现,不了解git命令的可以去了解下. 第一步:建立git仓库 cd到你的本地 ...

  6. es6中,promise使用过程的小总结

    参考资料传送门:戳一戳 1.是什么 Promise是异步编程的一种解决方案,有三种状态:pending(进行中).fulfilled(已成功)和rejected(已失败); 一般成功了状态用resol ...

  7. Django 练习班级管理系统四 -- 编辑班级

    修改 classes.html {% extends "layout.html" %} {% block css %} {% endblock %} {% block conten ...

  8. composer基本操作详解

    原文转自微信公众号:qq1005349393 Composer介绍 Composer 是 PHP 的一个包依赖管理工具.我们可以在项目中声明所依赖的外部工具库,Composer 会帮你安装这些依赖的库 ...

  9. Ubuntu14 关机重启、版本、网络、防火墙

    关机.重启: 立即关机:halt.poweroff.shutdown -h now 延迟关机:shutdown -h 10  十分钟后关机 立即重启:reboot.shutdown -r now 延迟 ...

  10. JS中的实例方法、静态方法、实例属性、静态属性

    一.静态方法与实例方法的例子: 我们先来看一个例子来看一下JS中的静态方法和实例方法到底是什么? 静态方法: function A(){} A.col='red'  //静态属性 A.sayMeS=f ...