Codeforces 71A 過長的單字 – 翻譯與解答

難易度 : 800

英文原文如下

Sometimes some words like “localization” or “internationalization” are so long that writing them many times in one text is quite tiresome.

Let’s consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.

This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters. That number is in decimal system and doesn’t contain any leading zeroes.

Thus, “localization” will be spelt as “l10n”, and “internationalization» will be spelt as “i18n”.

You are suggested to automatize the process of changing the words with abbreviations. At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.

中文翻譯

有時候有一些單字像是 “localization” 或是 “internationalization” 很長,多次在同一個文件中重複寫它們相當令人厭煩。

讓我們來定義何謂太長的單字,那就是超過10個字元的單字。所有過長的單字都應該被一種特殊的縮寫替換。

這個縮寫的模式是這樣的:我們寫下一個單詞的第一個字母和最後一個字母,然後在它們之間寫下第一個字母和最後一個字母之間的字母數量。另外這個數量是基於10進位制的,且不包含任何前導 0 。

所以,”localization” 的特殊縮寫為 “l10n”,”internationalization” 就會是 “i18n”。

你被建議要自動化整個單字轉換為縮寫的過程,太長的單字應該要用特殊的縮寫來替換,而沒有過長的單字則不要進行任何異動。

輸入

The first line contains an integer n (1 ≤ n ≤ 100). Each of the following n lines contains one word. All the words consist of lowercase Latin letters and possess the lengths of from 1 to 100 characters.

第一行包含了一個整數  n (1 ≤ n ≤ 100)。 接下來的 n 行都包含了一個單字。每個單字是由小寫的字母組成,且長度會在 1 到 100之間。

輸出

Print n lines. The i-th line should contain the result of replacing of the i-th word from the input data.

印出 n 行。 ith 行應該包含輸入資料 ith 的單字替換後的結果。

範例

Input4
word
localization
internationalization
pneumonoultramicroscopicsilicovolcanoconiosis
Outputword
l10n
i18n
p43s

解題思路

這一題其實要考得很簡單

就是將長度超過10的單字,留頭留尾,中間則是其餘字元的數量

所以我們就是要先判斷初始長度,就可以輕鬆解決這個問題了

C#解決方案

方案1

int n = int.Parse(Console.ReadLine());

for(int i=0;i<n;i++){
    string str = Console.ReadLine();
    if(str.Length>10){
        Console.WriteLine(str[0]+(str.Length-2).ToString()+str[str.Length-1]);
    } else {
        Console.WriteLine(str);
    }
}

Java解決方案

方案1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());

        for (int i = 0; i < n; i++) 
        {
            String str = scanner.nextLine();
            if(str.length()>10){
                System.out.println(str.charAt(0)+String.valueOf(str.length()-2)+str.charAt(str.length()-1));
            } else {
                System.out.println(str);
            }
        }
    }
}

Python3解決方案

方案1

n = int(input())

for i in range(n):
    x = input()
    if (len(x) > 10):
        print(x[0] + str(len(x)-2) + x[len(x)-1])
    else:
        print(x)

JavaScript解決方案

方案1

var n = readline();

for (var i = 0; i < n; i++) {
    var str = readline();
    if (str.length > 10) {
        print(str[0] + (str.length - 2) + str[str.length - 1]);
    } else {
        print(str);
    }
}


結論

跟之前的題目類似,比起程式上的問題,看完題目反而才是一種折磨…

明明沒幾行就可以說明了,硬是要用的這麼複雜

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

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

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

題目連結 : Problem – 71A – Codeforces

一些其他的文章

發佈留言

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