『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 236A,虽然很水,但有一个很简单的函数用起来方便
1 second
256 megabytes
standard input
standard output
Description
Those days, many boys use beautiful girls' photos as avatars in forums. So it is pretty hard to tell the gender of a user at the first glance. Last year, our hero went to a forum and had a nice chat with a beauty (he thought so). After that they talked very
often and eventually they became a couple in the network.
But yesterday, he came to see "her" in the real world and found out "she" is actually a very strong man! Our hero is very sad and he is too tired to love again now. So he came up with a way to recognize users' genders by their user names.
This is his method: if the number of distinct characters in one's user name is odd, then he is a male, otherwise she is a female. You are given the string that denotes the user name, please help our hero to determine the gender of this user by his method.
Input
The first line contains a non-empty string, that contains only lowercase English letters — the user name. This string contains at most 100 letters.
Output
If it is a female by our hero's method, print "CHAT WITH HER!" (without the quotes), otherwise, print "IGNORE HIM!" (without the quotes).
Sample Input
wjmzbmr
CHAT WITH HER!
xiaodao
IGNORE HIM!
sevenkplus
CHAT WITH HER!
Hint
For the first example. There are 6 distinct characters in "wjmzbmr". These characters are: "w", "j", "m",
"z", "b", "r". So wjmzbmr is a female and you should print "CHAT WITH HER!".
题目确实水啊,但如果用两层循环遍历可能很费时,虽然数据很小,很水。。介绍一种很好用的函数,这就是我写这篇博客的目的;
来看AC代码1;
<span style="font-family:KaiTi_GB2312;font-size:18px;color:#ff6666;">#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
char a[120];
scanf("%s",a);
int x=strlen(a);
sort(a,a+x);
int n=unique(a,a+x)-a;//一般与sort结合使用,字符也是可以排序的;
if(n%2)
printf("IGNORE HIM!\n");
else
printf("CHAT WITH HER!\n");
return 0;
}</span>
再看AC代码2:
#include<cstdio>//相比上面代码很麻烦吧,也很费时;
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
using namespace std;
const int N=100+50;
char a[N],b[N];
int main()
{
int x,i,j,sum;
while(~scanf("%s",a))
{
x=strlen(a);
sum=0;
for(i=0; i<x; i++)
{
int f=0;
for(j=0; j<i; j++)
{
if(a[j]==a[i])
{
f=1;
break;
}
}
if(!f)
sum++;
}
if(sum%2)
printf("IGNORE HIM!");
else
printf("CHAT WITH HER!\n");
}
return 0;
}
unique()函数是一个去重函数,STL中unique的函数 unique的功能是去除相邻的重复元素(只保留一个),
还有一个容易忽视的特性是它并不真正把重复的元素删除。他是c++中的函数,所以头文件要加#include<iostream.h>,具体用法如下:
int num[100];
unique(num,mun+n)返回的是num去重后的尾地址,之所以说比不真正把重复的元素删除,其实是,该函数把重复的元素一到后面去了,然后依然保存到了原数组中,然后返回去重后最后一个元素的地址,因为unique去除的是相邻的重复元素,所以一般用之前都会要排一下序。
结合代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <time.h>
using namespace std;
int main()
{
int a[120];
for(int i=0;i<5;i++)
scanf("%d",&a[i]);
int n=unique(a,a+5)-a;//这里n就相当于是去重后不同元素的个数;
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}//可以自己试试看看输出结果;
『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 236A,虽然很水,但有一个很简单的函数用起来方便的更多相关文章
- 『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 237C,素数打表,二分查找
C. Primes on Interval time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]CF-236B. Easy Number Challenge
B. Easy Number Challenge time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 『NYIST』第九届河南省ACM竞赛队伍选拔赛[正式赛二]-最小内积(第八届北京师范大学程序设计竞赛决赛)
H. 最小内积 Time Limit: 1000ms Memory ...
- 『NYIST』第九届河南省ACM竞赛队伍选拔赛[正式赛二]- Nearly Lucky Number(Codeforces Beta Round #84 (Div. 2 Only)A. Nearly)
A. Nearly Lucky Number time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 『NYIST』第九届河南省ACM竞赛队伍选拔赛[正式赛二]--Codeforces -35D. Animals
D. Animals time limit per test 2 seconds memory limit per test 64 megabytes input input.txt output o ...
- K Simple question (第十届山东理工大学ACM网络编程擂台赛 正式赛)
题解:素数筛+唯一分解定理 可以把素数筛那部分放到while之外,减小时间复杂度. #include <stdio.h> #include <stdlib.h> #includ ...
- F 阎小罗的Minimax (第十届山东理工大学ACM网络编程擂台赛 正式赛 )
题解:by Mercury_Lc 阎小罗的矩阵给的n和m都不超过300,枚举一下所有情况就可以了,用前缀和来储存.数组a[x][y]代表前x行前y列的和是多少,那么枚举每一种切割的方式就可以.注意一下 ...
- ACM竞赛高手比其他程序员水平高很多吗?
1. ACM是一种很直接的评价程序员水平的体系 2. ACM竞赛会带来很多机遇(深造or工作),同时又是一个不小的挑战 3. 为竞赛而竞赛的事情不可取 详细点击这里
- 第八届山东省ACM大学生程序设计竞赛个人总结
因为省赛,从开学紧张到5月7号.心思也几乎全放在ACM的训练上.因为我还是校台球协会的会长,所以台协还有一些事情需要忙,但是我都给延迟了.老会长一直在催我办校赛,但我一直说 等等吧,因为校赛只能在周六 ...
随机推荐
- UVALive - 6428(扩展欧几里德)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=48388 前段时间偶然碰到的一道题,今天突然想到没把它记录下来. 比较不错的扩 ...
- Masonry自动布局与UIScrolView适配
Masonry介绍 Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简洁明了 并具有高可读性 而且同时支持 iOS 和 Max OS X.可以通过cocoa ...
- 【学习笔记】深入理解js原型和闭包(11)——执行上下文栈
继续上文的内容. 执行全局代码时,会产生一个执行上下文环境,每次调用函数都又会产生执行上下文环境.当函数调用完成时,这个上下文环境以及其中的数据都会被消除,再重新回到全局上下文环境.处于活动状态的执行 ...
- 在vscode中显示空格和tab符号
转自:https://blog.csdn.net/bmzk123/article/details/86501706 使用python时最烦人的就是代码对齐,而且tab和空格还不一样,为了便于对其,希望 ...
- spring @value 为什么没有获取到值
1.配置文件的路径没有扫描到 2.注解的bean 不是通过spring托管的.bean 要通过spring 注解,引用的时候要用@Autowired 自动注入的bean 不要用new 出来的bean ...
- TFS2010单独安装配置tfs build server
记录一下确实很磨人. 同样硬件和软件环境的两台服务器,其中一台服务器很久之前就配置好了tfs2010 build ,然后最近想再配置一台tfs build server,但是按照以前的配置流程始终提示 ...
- apropos命令
apropos——查看配置文件功能 示例1: # apropos ifconfig 显示ifconfig配置文件的功能,类似于执行man命令时的NAME信息
- python大文件读取
python大文件读取 https://stackoverflow.com/questions/8009882/how-to-read-a-large-file-line-by-line-in-pyt ...
- app支付宝授权登录获取用户信息
由后台进行地址的拼接(前台进行授权) // 生成授权的参数 String sign = ""; Long userId1 = SecurityUser.getUserId(); S ...
- 一次执行两个npm "start": "concurrently 'npm:dev' 'npm:json-server'"
用的这个程序 concurrently 说是再有异步的时候,&& 就不好使,而且&& 也不能执行npm 只能执行命令 官方地址:https://www.npmjs.co ...