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)的算法对于这道题来说还是太慢了.所以要另外想一些方法.注意到序列中的所有元素都不相同,所以两个序列中数对 ...
随机推荐
- MDT rules实用
[Settings]Priority=DefaultProperties=MyCustomProperty [Default]OSInstall=YSkipBDDWelcome=YESSkipCapt ...
- Python【day 11】迭代器
迭代器-用 1.迭代器的概念 1.可迭代对象-iterable str.list.tuple.dict.set.open().range() 2.可迭代对象的概念: 其数据类型的执行方法中含有__it ...
- 把本地项目提交到GIT上
1.init之前 1.1 新建.gitignore文件 在提交之前.gitignore文件会把文件里面包含的内容都忽略掉 node_modules // webstorm里面的配置,别人不一定用 .i ...
- VUE-父组件和子组件
1.父组件 const cnp2 = Vue.extend({ template: ` <div> <h2>我是构造器2</h2> <cpn1>< ...
- Docker 清理日志
docker 长时间运行后,日志文件会逐渐变大可以使用下面命令进行清除 #!/bin/bash echo "==================== start clean docker c ...
- ZKEACMS 无法运行问题汇总
前言 如果你还不知道ZKEACMS,不妨先了解一下. ASP.NET MVC 开源建站系统 ZKEACMS 推荐,从此网站“拼”起来 官方地址:http://www.zkea.net/zkeacms ...
- 在VideoFileClip函数中获取“OSError:[WinError 6]句柄无效”
我正在使用python通过导入moviepy库创建一个程序,但收到以下错误: from moviepy.editor import VideoFileClip white_output = 'vide ...
- Golang 需要避免踩的 50 个坑(三)
前言 Go 是一门简单有趣的编程语言,与其他语言一样,在使用时不免会遇到很多坑,不过它们大多不是 Go 本身的设计缺陷.如果你刚从其他语言转到 Go,那这篇文章里的坑多半会踩到. 如果花时间学习官方 ...
- java XML解析防止外部实体注入
/** * 增加防止部实体注入逻辑 * <功能详细描述> * @param reader * @throws SAXException * @see [类.类#方法.类#成员] */ pu ...
- c# 第9节 数据类型之值类型
本节内容: 1:数据类型是什么 1:数据类型是什么 2:数据类型--值类型 3:值类型和引用类型的区分 画图现象: 3:值类型的种类 整数: 浮点数: 字符: