Problem solution · Python

ABC299 C — Dango

ABC299 C — Dango: a Python solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to KATO-Hiro AtCoder Solutions.

Technique
Direct simulation
Source
KATO-Hiro AtCoder Solutions
Length
53 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For ABC299 C — Dango, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 53 lines of Python from the credited upstream file abc299_c.py.
  • The implementation visibly relies on sequence storage.
  • 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.

Source

Code and credit

This code comes from KATO-Hiro AtCoder Solutions by KATO-Hiro and is used under the CC0-1.0 licence.

Full codeABC299 C — Dango · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  from typing import List  def run_length_encoding(iterable: list) -> List[list]:    '''    Args:        iterable: A list of numbers or strings.    Returns:        A list containing consecutive characters and their count.    See:    https://qiita.com/DaikiSuyama/items/07e237b7372e7c7b3432    '''     from itertools import groupby     results = [[key, len(list(group))] for key, group in groupby(iterable)]     return results  def main():    import sys     input = sys.stdin.readline     n = int(input())    s = list(input().rstrip())    t = run_length_encoding(s)     if t[0][0] == "o":        t = [['-', 0]] + t        n += 1     if t[-1][0] == "o":        t = t + [['-', 0]]        n += 1     ans = -1     for i, (si, count) in enumerate(t):        if si == "o":            if t[i - 1][1] >= 1 or t[i + 1][1] >= 1:                ans = max(ans, count)     print(ans)     if __name__ == "__main__":    main() 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗