『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 236A,虽然很水,但有一个很简单的函数用起来方便

时间:2023-01-16 15:03:43
A. Boy or Girl
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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

Input
wjmzbmr
Output
CHAT WITH HER!
Input
xiaodao
Output
IGNORE HIM!
Input
sevenkplus
Output
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:

『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 236A,虽然很水,但有一个很简单的函数用起来方便

#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;

}//可以自己试试看看输出结果;