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)的算法对于这道题来说还是太慢了.所以要另外想一些方法.注意到序列中的所有元素都不相同,所以两个序列中数对 ...
随机推荐
- HTML input属性详谈
value属性 value属性指定输入字段的初始值: <form> 名字:<br> <input type="text" name="you ...
- Git Error:There is no tracking information for the current branch.
在执行git pull的时候,提示当前branch没有跟踪信息: $> git pull There is no tracking information for the current bra ...
- ActiveMQ反序列化(CVE-2015-5254) && ActiveMQ任意文件写入 (CVE-2016-3088)
ActiveMQ 反序列化漏洞(CVE-2015-5254) 漏洞详情 ActiveMQ启动后,将监听61616和8161两个端口,其中消息在61616这个端口进行传递,使用ActiveMQ这个中间件 ...
- 用python登录12306 并保存cookie
一篇拿来记录的文章,是看其他博主写的,想在这记一下 import sys import time import requests from PIL import Image import json i ...
- maven 学习---部署基于Maven的war文件到Tomcat
在本教程中,我们将学习如何使用Maven的Tomcat插件打包并部署一个WAR文件到Tomcat(Tomcat的6和7. 要用到工具: Maven 3 Tomcat 6.0.37 Tomcat 7.0 ...
- day04 作业
一.简述Python的五大数据类型的作用.定义方式.使用方法: 数字类型 整型 作用:描述年龄 定义方式: x = 10 y = int('10') 使用方法: + - * / % // ** 如果需 ...
- 一篇文章搞定git
git学习 一.背景(当成故事去读) Linus 虽然创建了 Linux,但 Linux 的壮大是靠全世界热心的志愿者参与的,这么多人在世界各地为 Linux 编写代码,那 Linux 的代码是如何管 ...
- Java高级-反射
1.如何创建Class的实例 1.1过程:源文件经过编译(javac.exe)以后,得到一个或者多个.class文件..class文件经过运行(java.exe)这步,就需要进行类的加载(通过JVM的 ...
- java+selenium元素定位和元素操作
1.元素定位 ID定位元素: findElement(By.id(“”)); 通过元素的名称定位元素: findElement(By.name(“”)); 通过元素的html中的位置定位元素: fin ...
- django的几个常见命令、request请求取值形式、数据库连接、
django基础知识薄弱点 几个常见的命令 #创建django项目 django-admin startproject mysite #启动django项目 python manage.py runs ...