公式

B - 元気な応援メッセージ / Cheerful Support Message 解説 by admin

Qwen3-Coder-480B

Overview

For each visitor’s cheering message, if the number of exclamation marks ! it contains is \(K\) or more, it is considered an “energetic cheering message.” The problem asks us to count the number of such messages.

Analysis

In this problem, we need to count the number of exclamation marks contained in each message. This requires an operation that counts the occurrences of a specific character in a string.

A straightforward approach would be to examine each string character by character and manually count the number of ! marks. However, Python has a convenient method called str.count(), which allows us to easily obtain the number of occurrences of a specific character. This makes the implementation both simple and fast.

Looking at the constraints, since \(N\) and the length of each string are at most a few thousand, examining all messages once each is more than sufficient. Therefore, a simple brute-force approach without any special optimizations can solve the problem without issues.

Algorithm

  1. First, read the number of visitors \(N\) and the threshold value \(K\) from input.
  2. For each message \(S_i\), do the following:
    • Count the number of exclamation marks using S.count('!').
    • If the count is \(K\) or more, increment a counter by 1.
  3. Finally, output the value of the counter.

For example, given the following input:

3 2
Hello!!
Good!!!
Bye!
  • Hello!! → 2 ! marks → OK
  • Good!!! → 3 ! marks → OK
  • Bye! → 1 ! mark → NG

Therefore, the answer is \(2\).

Complexity

  • Time complexity: \(O(N \cdot |S|)\)
    (Since each string has a maximum length of \(|S|\), and we process \(N\) strings in total)
  • Space complexity: \(O(1)\)
    (Since we do not use any additional large data structures)

Implementation Notes

  • Using Python’s str.count() method makes it easy to find the number of occurrences of a specific character.
  • Since each message is given separated by newlines, we can read them sequentially by simply calling input() \(N\) times.
  • Note that the condition check is >= K (including the case where the count is exactly \(K\)).
## Source Code

```python
# 入力の受け取り
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 qwen3-coder-480b.

投稿日時:
最終更新: