Luogu 🟠 P14357 [CSP-J 2025] 拼数
題目的難度顏色使用 Luogu 上的分級,由簡單到困難分別為 🔴🟠🟡🟢🔵🟣⚫。
🔗 🟠 P14357 [CSP-J 2025] 拼数
Problem Statement
題目簡述
給定一個只包含小寫英文字母與數字的字串。可以使用其中任意多個數字,且每個位置的數字最多使用一次,將選出的數字任意重排後拼成一個正整數。
求所有能拼出的正整數中的最大值。
Constraints
約束條件
- 字串僅包含小寫英文字母與數字。
- 字串中至少包含一個 到 的數字,因此答案一定是正整數。
思路:貪心
由於在末尾加任何數字只會讓數值變大,因此使用所有數字一定是最優的。
接著考慮數字的排列順序。對於一個正整數,高位的權重最大,因此應該將數字按從大到小的順序排列。
不過數字只有 種,不需要排序,統計頻率後按降序展開即可做到 。
複雜度分析
- 時間複雜度:,其中 是字串長度, 是數字的種類數(這裡是 )
- 空間複雜度:
Code
1 | def solve(): |
寫在最後
Cover Image Credit
The cover image was created by @4AUB. All rights belong to the original artist.
It is used here only as a non-commercial cover illustration for this note. I do not claim ownership of the artwork.
If you are the copyright holder and believe this usage is inappropriate, please contact me by email or leave a comment. I will remove the image promptly.
本部落格所有文章除特別聲明外,均採用CC BY-NC-SA 4.0 授權協議。轉載請註明來源 十六個天亮!
評論
WalineGiscus



![Luogu 🟡 P14359 [CSP-J 2025] 异或和](https://i.pixiv.cat/img-master/img/2026/06/21/14/33/23/146273507_p0_master1200.jpg)
