- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 56 lines of Python from the credited upstream file abc380_c.py.
- The implementation visibly relies on sequence storage, ordered lookup.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3from typing import List4 5 6def run_length_encoding(iterable: list) -> List[list]:7 """8 Args:9 iterable: A list of numbers or strings.10 11 Returns:12 A list containing consecutive characters and their count.13 14 See:15 https:qiita.com/DaikiSuyama/items/07e237b7372e7c7b343216 """17 18 from itertools import groupby19 20 results = [[key, len(list(group))] for key, group in groupby(iterable)]21 22 return results23 24 25def main():26 import sys27 28 input = sys.stdin.readline29 30 n, k = map(int, input().split())31 s = input().rstrip()32 results = run_length_encoding(list(s))33 count = 034 pos = -135 36 for i, (key, _) in enumerate(results):37 if key == "1":38 count += 139 40 if k == count:41 pos = i42 break43 44 45 results[pos - 1], results[pos] = results[pos], results[pos - 1]46 ans = []47 48 for key, count in results:49 ans += [key] * count50 51 print("".join(ans))52 53 54if __name__ == "__main__":55 main()56