C - 文字列圧縮 / String Compression 解説 by admin 印刷ページ
Claude 4.5 OpusOverview
This problem asks you to count consecutive identical characters in a string and compress sections that repeat \(K\) or more times into the format “character+count”.
Analysis
Understanding the Problem
First, let’s verify the behavior with a concrete example.
- Input:
aaabbc, \(K = 2\) aaa→ 3 consecutive ≥ 2, so compress toa3bb→ 2 consecutive ≥ 2, so compress tob2c→ 1 consecutive < 2, so keep asc- Output:
a3b2c
Key Insights
- We need to group consecutive identical characters
- For each group, branch the processing based on whether the consecutive count is \(K\) or more
- This can be solved with a single scan of the string (\(O(N)\))
Comparison with Naive Approach
This problem is relatively simple, and the naive approach of “scanning from the beginning” directly becomes the optimal solution.
However, since repeated string concatenation can be slow, it’s more efficient to store results in a list and join them all at the end.
Algorithm
Initialization: Prepare a list
resultto store the output, and start from position \(i = 0\)Detecting Consecutive Sections: Count consecutive identical characters starting from position \(i\)
- Record the current character
current_char - Increment the counter while the next character is the same
- Record the current character
Compression Decision:
- If consecutive count
count≥ \(K\): Add in(character)(count)format - If consecutive count
count< \(K\): Add the original characters as-is
- If consecutive count
Position Update: Advance \(i\) by
countRepeat: Repeat steps 2-4 until the end of the string
Output Result: Join the list elements and output
Example Walkthrough
Input: aaabbc, \(K = 2\)
| \(i\) | Target | count | Decision | Added to result |
|---|---|---|---|---|
| 0 | aaa |
3 | 3 ≥ 2 | a3 |
| 3 | bb |
2 | 2 ≥ 2 | b2 |
| 5 | c |
1 | 1 < 2 | c |
Final result: a3b2c
Complexity
Time Complexity: \(O(|S|)\)
- Only one scan through the string is needed
- Each character is processed exactly once
Space Complexity: \(O(|S|)\)
- Size of the list storing the result
- In the worst case (no compression at all), it equals the input length
Implementation Notes
Adding to List and Final Join:
- In Python, repeated string concatenation is slow, so it’s more efficient to append to a list and join with
"".join()at the end
- In Python, repeated string concatenation is slow, so it’s more efficient to append to a list and join with
Method for Counting Consecutive Characters:
while i + count < n and s[i + count] == current_charprevents out-of-bounds access while counting
Using f-strings:
f"{current_char}{count}"concisely creates the “character+count” format
Handling Cases Below K:
current_char * countrestores the original consecutive string (e.g.,c * 1 = "c",b * 2 = "bb")
Source Code
def compress_string(s, k):
if not s:
return ""
result = []
i = 0
n = len(s)
while i < n:
current_char = s[i]
count = 1
# Count consecutive identical characters
while i + count < n and s[i + count] == current_char:
count += 1
# Compress if K or more times, otherwise keep as-is
if count >= k:
result.append(f"{current_char}{count}")
else:
result.append(current_char * count)
i += count
return "".join(result)
# Input
S = input()
K = int(input())
# Compress and output
print(compress_string(S, K))
This editorial was generated by claude4.5opus.
投稿日時:
最終更新: