A - 錬金術の材料 / Alchemy Materials Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
Count the number of gold, silver, and bronze items in the string, multiply each count by its respective value, and sum them up.
Analysis
Since the value of each material is determined solely by its type, the order of the materials does not affect the total.
Letting \(g, s,\) and \(c\) be the number of gold, silver, and bronze items respectively, the total value to find is:
\(3g + 2s + c\)
For example, if the string is GGSC, there are \(2\) gold, \(1\) silver, and \(1\) bronze, so the total is \(3 \times 2 + 2 \times 1 + 1 = 9\) points.
For this problem, a naive approach of scanning the string from the beginning and accumulating the value easily runs well within the time limit. No special optimizations are needed. In the provided code, Python’s count method is used to count the occurrences of each type.
Algorithm
- Read the number of materials \(N\) and the string \(S\).
- Count the number of gold items with
S.count('G')and multiply it by \(3\). - Count the number of silver items with
S.count('S')and multiply it by \(2\). - Count the number of bronze items with
S.count('C'). Since the value of bronze is \(1\) point, use it as is. - Add these together and output the result.
Complexity
- Time complexity: \(O(N)\)
Sincecountscans the entire string, each call takes \(O(N)\) time. It is executed 3 times, which is \(O(N)\) ignoring constant factors. - Space complexity: \(O(N)\)
\(O(N)\) space is used to store the input string. The additional space used besides that is \(O(1)\).
Implementation Notes
Gold is
G, silver isS, and bronze isC. Map each of them correctly to their respective values \(3, 2,\) and \(1\).countreturns the number of occurrences of the specified character. If a material is not present, it returns \(0\), so no special case handling is necessary.Although \(N\) is read from the input, it is not directly used in the calculation because
countinspects the entire string.Source Code
# 問題文から独立に実装
n = int(input())
s = input().strip()
print(3 * s.count('G') + 2 * s.count('S') + s.count('C'))
This editorial was generated by gpt-6-astra-medium.
posted:
last update: