Official

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

Gemini 3.0 Flash

Overview

Given \(N\) strings, we count the number of exclamation marks ! contained in each string. The problem asks us to find how many strings (energetic cheering messages) contain at least \(K\) exclamation marks.

Analysis

To solve this problem, we need to accurately determine “how many ! characters are contained” in each message \(S_i\).

  1. String traversal: The maximum length of a single message is \(10^3\). Checking each character from the beginning of the string and counting ! is fast enough.
  2. Constraint verification: The total number of messages is \(N \le 10^4\), and the length of each message is \(|S_i| \le 10^3\). In the worst case, we process \(10^4 \times 10^3 = 10^7\) characters in total. When processing in Python, it’s safer to use techniques like reading all input at once rather than repeatedly calling standard input().
  3. Special cases: Since the maximum message length is \(1000\), if \(K > 1000\), no message can satisfy the condition. In this case, outputting 0 immediately is also correct.

Algorithm

We find the solution using the following steps:

  1. Input acquisition: Read \(N, K\) and the \(N\) strings \(S_1, S_2, \dots, S_N\).
  2. Counting process: For each \(S_i\), count the number of ! using one of the following methods:
    • Use the string’s count('!') method.
    • Split the string by ! (using split) and calculate from the number of elements in the resulting list.
      • When the list obtained by s.split('!') has length \(m\), the number of ! contained is \(m - 1\).
      • Therefore, if m - 1 >= k, i.e., m > k, it is an “energetic cheering message”.
  3. Aggregation: Each time a message satisfying the condition is found, increment the answer variable energetic_count by \(1\).
  4. Output: Output the final energetic_count.

Complexity

  • Time complexity: \(O(\sum |S_i|)\)
    • Since we traverse all characters of all messages once, the time is proportional to the sum of character counts. This is at most around \(10^7\) characters, which fits well within the time limit.
  • Space complexity: \(O(\sum |S_i|)\)
    • This is the complexity when storing all input strings in memory.

Implementation Notes

  • Efficient input: By using sys.stdin.read().split(), you can obtain input separated by whitespace or newlines as a list all at once. This allows fast processing even for large amounts of input data.

  • Character counting optimization: Python’s split('!') method is implemented internally in C, making it very fast. Since we only need to compare list lengths for condition checking, this results in a simple and efficient implementation.

    Source Code

import sys

def main():
    # Read all input from standard input and split into tokens by whitespace.
    # This approach is efficient in Python and handles N, K, and all messages S_i.
    # Since S_i only contains letters, digits, and '!', it does not contain whitespace.
    data = sys.stdin.read().split()
    
    if not data:
        return

    # N: number of messages, K: minimum number of exclamation marks required.
    n = int(data[0])
    k = int(data[1])

    # The problem constraints state that each message S_i has a maximum length of 1000.
    # If the threshold K is greater than 1000, no message can possibly be "energetic".
    if k > 1000:
        print(0)
        return

    # We count "energetic" messages using a different approach from .count().
    # By splitting the string s by the exclamation mark '!', the resulting list 
    # of segments will have a length equal to (number of '!' characters) + 1.
    # Therefore, s.count('!') >= k is equivalent to len(s.split('!')) > k.
    
    energetic_count = 0
    # Messages are stored in the data list starting from index 2 up to index 2+n.
    for i in range(2, 2 + n):
        if i < len(data):
            # Applying the split-based counting logic
            if len(data[i].split('!')) > k:
                energetic_count += 1
    
    # Print the final count of energetic messages.
    print(energetic_count)

if __name__ == '__main__':
    main()

This editorial was generated by gemini-3-flash-preview.

posted:
last update: