B - 元気な応援メッセージ / Cheerful Support Message 解説 by admin
GPT 5.2 HighOverview
This is a problem where you count the number of exclamation marks ! in each message, consider it an “energetic cheer message” if it contains \(K\) or more, and find the total count of such messages.
Analysis
What needs to be done in this problem is simple: for each string \(S_i\), it suffices to check how many times ! appears.
For example, when \(K=3\):
- "Go!!" has 2 !s, so it is not energetic
- "Nice!!!" has 3 !s, so it is energetic
Such judgments are made.
There is no need for tricks like “concatenating all messages at once and counting,” and in fact, if you don’t judge each message individually, you won’t know “which messages satisfied the condition,” leading to wrong answers (WA).
Also, since \(N \le 10^4\) and each \(|S_i| \le 10^3\), the total is at most about \(10^7\) characters. Simply looking at each character once is well within the time limit (unlikely to TLE).
Algorithm
- Read \(N, K\) from input
- Initialize the answer
ans = 0 - Repeat for \(i=1\) to \(N\):
- Read string \(S_i\)
- Count the number of
!inS_i(in Python,S_i.count('!')) - If the count is \(K\) or more, increment
ansby 1
- Output
ans
Complexity
- Time complexity: \(O\left(\sum_{i=1}^{N} |S_i|\right)\)
(becausecountscans the entire string for each message) - Space complexity: \(O(1)\)
(since we process input strings one at a time and do not maintain any large additional arrays)
Implementation Notes
Since the input can have up to \(10^4\) lines, using
sys.stdin.readlineis faster and safer.Stripping only the trailing newline with
rstrip('\n')before counting prevents unintended characters from being included (though in this problem’s conditions, newlines do not affectcount('!'), so this is more of a safety measure).Using
s.count('!')allows for a short and error-free implementation.Source Code
import sys
def main():
input = sys.stdin.readline
N, K = map(int, input().split())
ans = 0
for _ in range(N):
s = input().rstrip('\n')
if s.count('!') >= K:
ans += 1
print(ans)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
投稿日時:
最終更新: