B - 元気な応援メッセージ / Cheerful Support Message 解説 by admin
Claude 4.5 OpusOverview
This is a problem about counting the number of “energetic cheering messages” that contain \(K\) or more exclamation marks ! among \(N\) messages.
Analysis
The key insights for this problem are as follows:
Each message can be evaluated independently: There’s no need to consider relationships between messages; we can simply examine them one by one in order.
The evaluation condition is simple: For each message, we just need to count the occurrences of
!and check whether the count is \(K\) or more.
Thinking Through a Concrete Example
For example, suppose \(N = 3\), \(K = 2\) with the following messages:
- Ganbare!! → 2 ! characters → \(2 \geq 2\), so it’s “energetic” ✓
- Hello → 0 ! characters → \(0 \geq 2\) is false, so it’s “not energetic” ✗
- Go!!!Go! → 4 ! characters → \(4 \geq 2\), so it’s “energetic” ✓
In this case, the answer is \(2\).
Why a Naive Approach Is Sufficient
In this problem, each message has a maximum length of \(10^3\), and there are at most \(10^4\) messages. So even if we examine every character of every message, it takes at most \(10^3 \times 10^4 = 10^7\) operations. This is well within the range that can be processed quickly enough.
Algorithm
- Initialize a variable
countto \(0\) to record the number of “energetic cheering messages” - For each of the \(N\) messages \(S_i\), repeat the following:
- Count the number of
!characters in string \(S_i\) - If the count of
!is \(K\) or more, incrementcountby \(1\)
- Count the number of
- Output the final value of
count
In Python, you can easily count the number of ! characters in a string \(S\) using the string method S.count('!').
Complexity
- Time complexity: \(O(N \times L)\)
- \(L\) is the maximum length of each message (at most \(10^3\))
- This is because the
count()method scans all characters for each message
- Space complexity: \(O(L)\)
- Since we read and process each message one at a time, we only need space to store one message
Implementation Notes
By utilizing Python’s
count()method, you can concisely obtain the occurrence count of a specific characterIf you read and process messages one at a time, there’s no need to store all messages in an array, which improves memory efficiency
Note that the condition is “\(K\) or more”, so use
>=(not>)Source Code
N, K = map(int, input().split())
count = 0
for _ in range(N):
S = input()
if S.count('!') >= K:
count += 1
print(count)
This editorial was generated by claude4.5opus.
投稿日時:
最終更新: