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.
- Define the state carried into one recursive or stack frame.
- Mark or choose the current state before exploring children.
- 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.
Use this to learn the idea, then write your own version.
12 3 4def main():5 from collections import defaultdict6 import sys7 8 sys.setrecursionlimit(10 ** 8)9 input = sys.stdin.readline10 11 n, m = map(int, input().split())12 s = [input().rstrip() for _ in range(n)]13 t = defaultdict(int)14 15 for _ in range(m):16 ti = input().rstrip()17 t[ti] += 118 19 remain = 16 - (n - 1) 20 21 for si in s:22 remain -= len(si)23 24 underscore = "_"25 used = [False] * n26 27 28 29 def dfs(i: int, cur_s: str, underscore_count: int) -> bool:30 31 if i == n:32 33 if len(list(cur_s)) < 3:34 return False35 36 if t[cur_s] != 0:37 return False38 39 print(cur_s)40 exit()41 42 43 if underscore_count > 0:44 if dfs(i, cur_s + underscore, underscore_count - 1):45 return True46 47 for j in range(n):48 if not used[j]:49 used[j] = True50 51 if dfs(i + 1, cur_s + underscore + s[j], underscore_count):52 return True53 54 used[j] = False55 56 return False57 58 for i in range(n):59 used[i] = True60 dfs(1, s[i], remain)61 used[i] = False62 63 print(-1)64 65 66if __name__ == "__main__":67 main()68