UVA 10789 题解
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 题解的更多相关文章
- UVA 10131题解
第一次写动态规划的代码,整了一天,终于AC. 题目: Question 1: Is Bigger Smarter? The Problem Some people think that the big ...
- 位运算基础(Uva 1590,Uva 509题解)
逻辑运算 规则 符号 与 只有1 and 1 = 1,其他均为0 & 或 只有0 or 0 = 0,其他均为1 | 非 也就是取反 ~ 异或 相异为1相同为0 ^ 同或 相同为1相异为0,c中 ...
- 【OI】计算分子量 Molar mass UVa 1586 题解
题目:(由于UVa注册不了,还是用vjudge) https://vjudge.net/problem/UVA-1586 详细说明放在了注释里面.原创. 破题点在于对于一个元素的组合(元素+个数),只 ...
- UVa 12171 题解
英文题面不怎么友好,大家还是自行通过紫书了解题面吧... 解题思路: 1. 面对500 ^ 3的数据范围,我们需要先用离散化解决掉爆空间的问题. 2. 由于我们要求的总体积包括内空部分的体积,我们可以 ...
- UVA题解三
UVA题解三 UVA 127 题目描述:\(52\)张扑克牌排成一列,如果一张牌的花色或者数字与左边第一列的最上面的牌相同,则将这张牌移到左边第一列的最上面,如果一张牌的花色或者数字与左边第三列的最上 ...
- UVA题解二
UVA题解二 UVA 110 题目描述:输出一个Pascal程序,该程序能读入不多于\(8\)个数,并输出从小到大排好序后的数.注意:该程序只能用读入语句,输出语句,if语句. solution 模仿 ...
- [题解]UVa 11082 Matrix Decompressing
开始眨眼一看怎么也不像是网络流的一道题,再怎么看也觉得像是搜索.不过虽然这道题数据范围很小,但也不至于搜索也是可以随随便便就可以过的.(不过这道题应该是special judge,因为一题可以多解而且 ...
- [题解]UVa 10891 Game of Sum
在游戏的任何时刻剩余的都是1 - n中的一个连续子序列.所以可以用dp[i][j]表示在第i个数到第j个数中取数,先手的玩家得到的最大的分值.因为两个人都很聪明,所以等于自己和自己下.基本上每次就都是 ...
- [题解]UVa 10635 Prince and Princess
讲一下题目大意,就是有两个长度为p + 1和q + 1的序列,求它们的LCS. 如果用O(pq)的算法对于这道题来说还是太慢了.所以要另外想一些方法.注意到序列中的所有元素都不相同,所以两个序列中数对 ...
随机推荐
- Python中的常见特殊方法—— del方法
__del__() 方法用于销毁Python对象——在任何Python对象将被系统回收的时候,系统都会自动调用这个方法.但是不要以为对一个变量执行del操作,该变量引用的对象就会被回收,当然不是,如果 ...
- 纯C语言实现链栈
#include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct StackNode{ E ...
- C# 获取社会统一信用代码
时间不多,废话少说: 网络请求代码如下: using System; using System.Collections.Generic; using System.Linq; using System ...
- affine_trans_pixel 和 affine_trans_point_2d的区别
affine_trans_pixel 和 affine_trans_point_2d的不同在于所使用的坐标系原点不同,affine_trans_pixel 使用的是像素坐标系, 即原点位于图像的左上角 ...
- 剑指offer 9-10:青蛙跳台阶与Fibonacii数列
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). 问题分析 我们将跳法个数y与台阶数n视为一个函数关系,即y=f(n). ...
- nodejs简单http日志存储
nodejs实现简单http日志存储 /* 日志存储: 202.189.63.115 - - [31/Aug/2008:15:42:31 +0800] "GET / HTTP/1.1&quo ...
- Django类
django 1.中间件 中间件一般做认证或批量请求处理,django中的中间件,其实是一个类,在请求和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法, 如请求过来 执行p ...
- 网络流之最大流Dinic --- poj 1459
题目链接 Description A power network consists of nodes (power stations, consumers and dispatchers) conne ...
- 201871010132-张潇潇-《面向对象程序设计(java)》第七周总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...
- CentOS 8 正式发布!
CentOS 8 正式发布! CentOS 8 和 RedHat Enterprise Linux 8 发行的版本是一致的,都是基于 Fedora 28 和 内核 4.18.支持传统的.新兴的工作负载 ...