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 from itertools import product def main(): n = int(sys.stdin.readline()) strings = [sys.stdin.readline().strip() for _ in range(n)] lowercase = 'abcdefghijklmnopqrstuvwxyz' L = 1 while True: max_possible = 26 ** L substrings = set() found_max = False for s in strings: m = len(s) if m < L: continue for i in range(m - L + 1): substr = s[i:i+L] if substr not in substrings: substrings.add(substr) if len(substrings) == max_possible: found_max = True break if found_max: break if len(substrings) < max_possible: # Generate the answer char_suffixes = {} for substr in substrings: c = substr[0] suffix = substr[1:] if c not in char_suffixes: char_suffixes[c] = set() char_suffixes[c].add(suffix) # Check each character in order for c in lowercase: current_suffixes = char_suffixes.get(c, set()) required = 26 ** (L - 1) if len(current_suffixes) < required: # Generate all possible suffixes if L == 1: print(c) return for chars in product(lowercase, repeat=L-1): suffix = ''.join(chars) if suffix not in current_suffixes: print(c + suffix) return # Shouldn't reach here assert False else: L += 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.