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
import sys def main(): import sys from sys import stdin N = int(stdin.readline()) S_list = [stdin.readline().strip() for _ in range(N)] if not S_list: print('a') return max_len = max(len(s) for s in S_list) for L in range(1, max_len + 2): if L > max_len: print('a' * L) return forbidden = set() for s in S_list: s_len = len(s) if s_len < L: continue for i in range(s_len - L + 1): substr = s[i:i+L] forbidden.add(substr) if len(forbidden) < (26 ** L): # Build trie trie = {} for s in forbidden: node = trie for c in s: if c not in node: node[c] = {} node = node[c] # Find lex smallest stack = [(trie, [], 0)] result = None while stack: node, prefix, level = stack.pop() if level == L: continue # Iterate in reverse to process 'a' first for c in reversed('abcdefghijklmnopqrstuvwxyz'): next_prefix = prefix + [c] if c not in node: remaining = L - level - 1 result = ''.join(next_prefix) + 'a' * remaining print(result) return else: stack.append((node[c], next_prefix, level + 1)) if result is not None: break # Fallback, though it shouldn't be needed print('a' * L) return # Fallback if all lengths up to max_len+1 are covered (impossible) print('a' * (max_len + 1)) if __name__ == '__main__': main()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.