Codeforces 236A 男孩或女孩 – 翻譯與解答

難易度 : 800

英文原文如下

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.

中文翻譯

那些日子,許多男生使用漂亮的女生的照片作為他們論壇的頭像,所以這使得第一眼很難分辨使用者的性別。去年,我們的主角進入了一個論壇並與一位美女(他這麼以為)有一次美好的聊天。再經過一段時間頻繁的聯絡下,他們最終成為了一對網路上的情侶。

但在昨天的時候,他跑去現實世界見「她」,卻發現「她」其實是一個很壯的男性! 我們的男主角非常難過而且他有好一段日子都不想在愛了。因此,他想出了一種方式來辨別使用者的性別 – 他們的使用者名稱。

這裡是他的方法 : 要是一個使用者名稱唯一的字母是奇數,那他就是男生,反之則為女生。會給予你一個字串代表使用者的名稱,請幫助我們的主角來決定該使用者的性別 (用他的方式)。

輸入

The first line contains a non-empty string, that contains only lowercase English letters — the user name. This string contains at most 100 letters.

第一行包含了一個非空的字串,由小寫英文字母組成 – 使用者名稱。這個字串包含了最多100個字母。

輸出

If it is a female by our hero’s method, print “CHAT WITH HER!” (without the quotes), otherwise, print “IGNORE HIM!” (without the quotes).

要是這個模式得到女生的結果,印出”CHAT WITH HER!” (不用引號)。若非,則印出 “IGNORE HIM!” (不用引號)

範例

輸入wjmzbmr
輸出CHAT WITH HER!
輸入xiaodao
輸出IGNORE HIM!
輸入sevenkplus
輸出CHAT WITH HER!

筆記

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!”.

在第一個範例中,”wjmzbmr” 有六個唯一的字元,它們分別是”w”, “j”, “m”, “z”, “b”, “r”。所以 wjmzbmr 是一個女生,而你應該印出 “CHAT WITH HER!” (跟她聊天)


解題思路

在第一個解答中,我們使用 HashSet來儲存那些唯一的字母,就可以簡單的計算它們的數量了。

C#解決方案

方案1

string username = Console.ReadLine();
HashSet<char> hSet = new HashSet<char>();

char[] charArr = username.ToCharArray();

foreach (char c in charArr)
{
    hSet.Add(c);
}

string res = hSet.Count%2==0? "CHAT WITH HER!" : "IGNORE HIM!";

Console.WriteLine(res);

方案2 – LINQ Distinct

string username = Console.ReadLine();

int cnt = username.Distinct().Count();

string res = cnt%2==0? "CHAT WITH HER!" : "IGNORE HIM!";

Console.WriteLine(res);

而當你使用LINQ 的 Distinct 一切變得更簡單了!

Java解決方案

方案1

import java.util.HashSet;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String username = scanner.nextLine();
        HashSet<Character> hSet = new HashSet<>();

        char[] charArr = username.toCharArray();

        for (char c : charArr) {
            hSet.add(c);
        }

        String res = hSet.size() % 2 == 0 ? "CHAT WITH HER!" : "IGNORE HIM!";
        System.out.println(res);
    }
}

方案2 – streaming

import java.util.Scanner;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String username = scanner.nextLine();
        long cnt = username.chars()
                .mapToObj(c -> (char) c)
                .distinct()
                .count();

        String res = cnt % 2 == 0 ? "CHAT WITH HER!" : "IGNORE HIM!";
        System.out.println(res);
    }
}

與C#的 LINQ 類似,Java也可以使用streaming來輕鬆取得唯一並計算數量


結論

這絕對不是一個可以正確辨別性別的方式…

🧡如果這篇文章有幫上你的一點點忙,那是我的榮幸

🧡收藏文章或幫我點個廣告,那都是對我的支持

✅如有任何疑問,歡迎透過留言或messenger讓我知道 !

題目連結 : Problem – 236A – Codeforces

一些其他的文章

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *