Codeforces 4C Registration system 註冊系統

難易度 : 1300

英文原文如下

A new e-mail service “Berlandesk” is going to be opened in Berland in the near future. The site administration wants to launch their project as soon as possible, that’s why they ask you to help. You’re suggested to implement the prototype of site registration system. The system should work on the following principle.

Each time a new user wants to register, he sends to the system a request with his name. If such a name does not exist in the system database, it is inserted into the database, and the user gets the response OK, confirming the successful registration. If the name already exists in the system database, the system makes up a new user name, sends it to the user as a prompt and also inserts the prompt into the database. The new name is formed by the following rule. Numbers, starting with 1, are appended one after another to name (name1, name2, …), among these numbers the least i is found so that namei does not yet exist in the database.

中文翻譯

一個新的Email 服務 “Berlandesk” 在最近就要在 Berland 開放了。網站管理者希望將他們的項目盡可能早的上線。因此他們請求您的幫助。您被建議來實現整個註冊系統的原型。該系統應按照以下原則來運作。

每次新用戶想要註冊時,他向系統發送一個帶有他的名字的請求。如果在系統數據庫中不存在這樣的名字,則將其插入數據庫,並返回確認成功註冊的“OK”回復。如果名字已經存在於系統數據庫中,系統將為該用戶生成一個新的用戶名,將其作為提示發送給用戶,並將提示也插入到數據庫中。新名稱的形成遵守以下規則。數字從1開始,依次附加到名字(name1,name2,…),在這些數字中找到最小的i,也就是namei在數據庫中尚不存在。

輸入

The first line contains number n (1 ≤ n ≤ 105). The following n lines contain the requests to the system. Each request is a non-empty line, and consists of not more than 32 characters, which are all lowercase Latin letters.

第一行包含了一個數字 n (1 ≤ n ≤ 105)。將下來的 n 行包含了對系統的請求們。每一個請求都是一行非空白,且由不超過32的字元的小寫拉丁字母組成。

輸出

Print n lines, which are system responses to the requests: OK in case of successful registration, or a prompt with a new name, if the requested name is already taken.

印出 n 行,對應的就是系統對每個請求的答覆,要是註冊成功則回傳 “OK”,或是一個新的名字的提示,要是名字已經被取走了。

範例

輸入4
abacaba
acaba
abacaba
acab
輸出OK
OK
abacaba1
OK

因為abacaba已經被取過了,故第二個出現的就要在最後面加1。

輸入6
first
first
second
second
third
third
輸出OK
first1
OK
second1
OK
third1

解題思路

這一題,我們可以輕鬆地使用Dictionary來儲存名字以及其出現的次數。

C#解決方案

方案1

int n = int.Parse(Console.ReadLine());  //total count of names
Dictionary<string,int> dict =   new Dictionary<string,int>();

for(int i = 0; i<n ; i++)
{
    string name = Console.ReadLine();
    if (!dict.ContainsKey(name))
    {
        Console.WriteLine("OK");
        dict.Add(name, 1);
    }
    else
    {
        Console.WriteLine(name+dict[name].ToString());
        dict[name] = dict[name]+1;
    }
}

就是這麼簡單,Dict的Key儲存名字,Value則是次數(會隨出現而增加)


結論

這是第一次解決第三題!! 雖然近期第一次參加線上比賽時,連第一題都沒通過(只有通過pretest,後續的testcases 就出錯了…)

這一題出自於比賽 – Dashboard – Codeforces Beta Round 4 (Div. 2 Only) – Codeforces

然後這裡看這一系列的其他題目

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

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

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

題目連結 : Problem – 4C – Codeforces

一些其他的Codeforces文章

發佈留言

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