Problem solution · Python

ABC268 D — Unique Username

ABC268 D — Unique Username: a Python solution using depth-first search. Learn the idea, check the complexity, and read the full code, with credit to KATO-Hiro AtCoder Solutions.

Technique
Depth-first search
Source
KATO-Hiro AtCoder Solutions
Length
68 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

Depth-first search

For ABC268 D — Unique Username, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.

  1. Define the state carried into one recursive or stack frame.
  2. Mark or choose the current state before exploring children.
  3. Combine child results or undo the choice when the branch finishes.

Code notes

  • 68 lines of Python from the credited upstream file abc268_d.py.
  • The implementation visibly relies on sequence storage, hash lookup, ordered lookup.
  • No explicit loop blocks detected, together with recursive traversal.

Complexity

Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.

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 codeABC268 D — Unique Username · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  def main():    from collections import defaultdict    import sys     sys.setrecursionlimit(10 ** 8)    input = sys.stdin.readline     n, m = map(int, input().split())    s = [input().rstrip() for _ in range(n)]    t = defaultdict(int)     for _ in range(m):        ti = input().rstrip()        t[ti] += 1        remain = 16 - (n - 1)  # _を複数追加する回数     for si in s:        remain -= len(si)     underscore = "_"    used = [False] * n     # ありうる文字列のパターンをDFSで全探索    # 使用した文字の数、現在の文字列、_の残りの数    def dfs(i: int, cur_s: str, underscore_count: int) -> bool:        # 基本ケース        if i == n:            # コーナーケース: 3文字未満            if len(list(cur_s)) < 3:                return False                        if t[cur_s] != 0:                return False                        print(cur_s)            exit()            # 再帰ケース        if underscore_count > 0:            if dfs(i, cur_s + underscore, underscore_count - 1):                return True                for j in range(n):            if not used[j]:                used[j] = True                 if dfs(i + 1, cur_s + underscore + s[j], underscore_count):                    return True                 used[j] = False         return False     for i in range(n):        used[i] = True        dfs(1, s[i], remain)        used[i] = False        print(-1)  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 ↗