problem AOR Ika wants to create a strong password that consists only of lowercase…
- User
problem
AOR Ika wants to create a strong password that consists only of lowercase letters. AOR Ika-chan, who was given an example of of dangerous passwords by a friend, decided to create a password that meets all of the following conditions.
- The length is at least one character.
- Different from any contiguous substring of any dangerous password.
- This is the shortest character string that meets the conditions 1 and 2.
- This is the character string that comes to the beginning when arranged in lexicographic order while satisfying the conditions 1, 2, and 3.
Write a program to generate a strong password on behalf of AOR Ika-chan. input
Input is given from standard input in the following format.
Assistant
n = int(input()) strings = [input().strip() for _ in range(n)] # Check for k=1 chars = set() for s in strings: chars.update(s) if len(chars) < 26: for c in 'abcdefghijklmnopqrstuvwxyz': if c not in chars: print(c) exit() # Check for k >= 2 max_len = max(len(s) for s in strings) if strings else 0 for k in range(2, max_len + 2): forbidden = set() for s in strings: l = len(s) if l < k: continue for i in range(l - k + 1): forbidden.add(s[i:i+k]) if len(forbidden) < (26 ** k): # Build trie trie = {} for substr in forbidden: node = trie for c in substr: if c not in node: node[c] = {} node = node[c] # Find minimal missing string res = [] found = False def dfs(node, depth): nonlocal found if depth == k: return False for c in 'abcdefghijklmnopqrstuvwxyz': if c not in node: res.append(c) res.extend(['a'] * (k - depth -1)) found = True return True res.append(c) if dfs(node[c], depth + 1): return True res.pop() return False dfs(trie, 0) print(''.join(res)) exit()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.