Codeforces 41A Translation 翻譯

難易度 : 800

英文原文如下

The translation from the Berland language into the Birland language is not an easy task. Those languages are very similar: a berlandish word differs from a birlandish word with the same meaning a little: it is spelled (and pronounced) reversely. For example, a Berlandish word code corresponds to a Birlandish word edoc. However, it’s easy to make a mistake during the «translation». Vasya translated word s from Berlandish into Birlandish as t. Help him: find out if he translated the word correctly.

中文翻譯

將 Berland 語翻譯成 Birland 語不是個簡單的工作。這些語言非常相似,兩者儘管有相同意思的單字卻有著拼字上相反的特性(當然發音也是)。舉例來說,一個 Berland 語的單字 code 對應到的就是 Birland 語的 edoc。然後,在«翻譯»的過程中很容易出錯。 Vasya 正在接 Berland 語的單字 s 翻譯成 Birland 語的單字 t 。幫助他,確認他的翻譯是否正確。

輸入

The first line contains word s, the second line contains word t. The words consist of lowercase Latin letters. The input data do not consist unnecessary spaces. The words are not empty and their lengths do not exceed 100 symbols.

第一行包含了一個單字 s ,第二行則包含了單字 t 。 單字們由小寫字母組成,不包含了不需要的空白。單字不是空白且他們的長度不會超過100的字元。

輸出

If the word t is a word s, written reversely, print YES, otherwise print NO.

要是單字 t 是 單字 s 倒反過來的寫法,印出 YES,反之則 NO。

範例

輸入code
edoc
輸出YES
輸入abb
aba
輸出NO
輸入code
code
輸出NO

解題思路

在這個問題中,唯一要做的就是找到反轉一個字串的方式。

C#解決方案

方案1

string text1 = Console.ReadLine();
string text2 = Console.ReadLine();

char[]charArr1 = text1.ToCharArray();
char[]charArr2 = text2.ToCharArray();

Array.Reverse(charArr1);

Console.WriteLine(charArr1.SequenceEqual(charArr2) ? "YES" : "NO" );

注意 : C#比較兩個Array的時候,不能用 == 運算子是因為那樣比較的是兩個陣列的引用 (reference),而不是他們實際的內容。所以這裡要用 SequenceEqual 裡的相等比較子來判斷是否相等 。

Java解決方案

方案1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String text1 = scanner.nextLine();
        String text2 = scanner.nextLine();
        
        text1 = new StringBuilder(text1).reverse().toString();
        
        System.out.println(text1.equals(text2) ? "YES" : "NO");
    }
}

結論

在這一題中,我會學到各語言該如何反轉字串

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

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

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

題目連結 : Problem – 41A – Codeforces

一些其他的Codeforces文章

發佈留言

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