Codeforces 141A Amusing Joke 有趣的笑話

題目說明

難易度 : 800

英文原文如下

So, the New Year holidays are over. Santa Claus and his colleagues can take a rest and have guests at last. When two “New Year and Christmas Men” meet, thear assistants cut out of cardboard the letters from the guest’s name and the host’s name in honor of this event. Then the hung the letters above the main entrance. One night, when everyone went to bed, someone took all the letters of our characters’ names. Then he may have shuffled the letters and put them in one pile in front of the door.

The next morning it was impossible to find the culprit who had made the disorder. But everybody wondered whether it is possible to restore the names of the host and his guests from the letters lying at the door? That is, we need to verify that there are no extra letters, and that nobody will need to cut more letters.

Help the “New Year and Christmas Men” and their friends to cope with this problem. You are given both inscriptions that hung over the front door the previous night, and a pile of letters that were found at the front door next morning.

中文翻譯

所以,新年假期已經結束了。聖誕老人和他的同事們終於可以休息,並且可以招待客人了。當兩個「新年和聖誕老人」相遇時,他們的助手會用硬紙板切出客人和主人名字的字母以紀念這一事件。然後把這些字母掛在主入口上方。有一個晚上,當每個人都上床睡覺時,有人拿走了我們角色名字的所有字母。然後他可能洗牌了這些字母,把它們放在門前的一堆裡。

第二天早上,已經不可能找到製造混亂的罪犯了。但大家都想知道是否可以從門口找到的字母還原主人和客人的名字?也就是說,我們需要驗證是否有多餘的字母,以及是否有人需要再切割更多的字母。

幫助「新年和聖誕老人」及其朋友解決這個問題。會給予你前一天晚上掛在前門上的字樣和第二天早上在前門找到的一堆字母。

輸入

The input file consists of three lines: the first line contains the guest’s name, the second line contains the name of the residence host and the third line contains letters in a pile that were found at the door in the morning. All lines are not empty and contain only uppercase Latin letters. The length of each line does not exceed 100.

輸入文件由三行組成:第一行包含客人的名字,第二行包含住宅主人的名字,第三行包含早上在門口找到的一堆字母。所有行都不為空,只包含大寫拉丁字母。每行的長度不超過100個字元。

輸出

Print “YES” without the quotes, if the letters in the pile could be permuted to make the names of the “New Year and Christmas Men”. Otherwise, print “NO” without the quotes.

如果堆中的字母可以重排以製作出「新年和聖誕老人」的名字,印出 “YES”(不含雙引號)。否則,印出”NO”(不含雙引號)。

範例

輸入SANTACLAUS
DEDMOROZ
SANTAMOROZDEDCLAUS
輸出YES
輸入PAPAINOEL
JOULUPUKKI
JOULNAPAOILELUPUKKI
輸出NO
輸入BABBONATALE
FATHERCHRISTMAS
BABCHRISTMASBONATALLEFATHER
輸出NO

筆記

In the first sample the letters written in the last line can be used to write the names and there won’t be any extra letters left.

In the second sample letter “P” is missing from the pile and there’s an extra letter “L”.

In the third sample there’s an extra letter “L”.

在第一個範例中,最後一行中寫的字母可以用來寫出名字,並且不會有任何多餘的字母。

在第二個範例中,堆中缺少字母「P」,並且多了一個字母「L」。

在第三個範例中,多了一個字母「L」。

3秒鐘快速了解

給予三個字串,判斷前兩個字串打亂後是否為第三個字串,多一個或少一個都不行!!!


解題思路

需要判斷有字元跟數量的,用Dictionary肯定會是我第一個想到的解法

C#解決方案

方案1

Dictionary<char,int> dict =   new Dictionary<char,int>();


string str1 = Console.ReadLine();
string str2 = Console.ReadLine();

foreach(var tmp in str1)
{
    if (!dict.ContainsKey(tmp))
    {
        dict.Add(tmp, 1);
    }
    else
    {
        dict[tmp] = dict[tmp]+1;
    }
}

foreach(var tmp in str2)
{
    if (!dict.ContainsKey(tmp))
    {
        dict.Add(tmp, 1);
    }
    else
    {
        dict[tmp] = dict[tmp]+1;
    }
}

string strChk = Console.ReadLine();
bool notfound = false;

foreach(var tmp in strChk)
{
    if (!dict.ContainsKey(tmp))
    {
        notfound = true;
        break;
    }
    else
    {
        var val = dict[tmp]-1;
        dict[tmp] = val;
        if(val==0){
            dict.Remove(tmp);
        }
    }
}

Console.WriteLine((dict.Count == 0 && !notfound) ? "YES" : "NO");

如果某個字母的出現次數等於0,我們會從字典中刪除它,這樣我們就可以確保門口的堆中的每個字母都與掛在入口上的名字中的字母匹配。

如果在字典中找不到任何字母(表示它是多餘的字母),則將 ‘notfound’ 設置為 true。

如果字典為空(表示所有必要的字母都被考慮在內)且 ‘notfound’ 為 false(表示找不到多餘的字母),我們就可以得出結論:這兩個名字是可以復原的~


結論

這一題肯定是英文的笑話,但我完全看不懂啊啊啊~

講到笑話,就讓我想到一個最近聽到的

「青蛙吃什麼會噎到?」

「南瓜(難呱)」

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

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

🫵徵求友站連結(需審核)或是任何類型的合作機會

題目連結 : Problem – 141A – Codeforces

類似題目

一些其他的Codeforces文章

發佈留言

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