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 \(S\), multiply each by its respective value, and sum them up.
Analysis
The value of a material depends solely on its type, and the order does not matter. Therefore, if we let \(g\) be the count of gold, \(sv\) be the count of silver, and \(c\) be the count of bronze, the required total is
\(3g + 2sv + c\)
For example, when \(S =\) 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 checking the string character by character from the beginning is sufficient. Since \(N \leq 100\), checking every character once will easily finish well within the time limit. No special optimizations are necessary.
Algorithm
- Read \(N\) and the string \(S\).
- Initialize the variables
g,sv, andc, representing the counts of gold, silver, and bronze, to \(0\). - For each character in the string, perform the following:
- If it is
G, incrementgby \(1\). - If it is
S, incrementsvby \(1\). - Otherwise, increment
cby \(1\).
- If it is
- Output
g * 3 + sv * 2 + c.
Since each material is counted exactly once, each variable will end up holding the correct count for that material type. Multiplying each count by its value and summing them gives the total value of all materials.
Complexity
- Time Complexity: \(O(N)\)
We inspect each character of the string once. - Space Complexity: \(O(N)\)
Required to store the input string. Excluding the storage of the string, the auxiliary space is \(O(1)\) for the counter variables, etc.
Implementation Notes
In C++, string indices are \(0\)-based, so iterate over the range
i = 0toi < n.Since the characters in \(S\) are only
G,S, andC, any character that is neitherGnorScan be determined to beC.The maximum possible total is \(3N \leq 300\), which occurs when all materials are gold. This can be handled comfortably using the C++
inttype.Source Code
// 問題文から独立に実装: 種類ごとに数えてから重みを掛ける
#include <iostream>
#include <string>
int main() {
int n; std::string s; std::cin >> n >> s;
int g = 0, sv = 0, c = 0;
for (int i = 0; i < n; ++i) { if (s[i] == 'G') g++; else if (s[i] == 'S') sv++; else c++; }
std::cout << g * 3 + sv * 2 + c * 1 << std::endl;
}
This editorial was generated by gpt-6-astra-medium.
posted:
last update: