We need to find all strings of length N (1 <= N <= 10) over lowercase letters a..z that are in normal form: for every string t isomorphic to s, s <= t (lexicographically). So s must be the lexicographically smallest among its isomorphism class.
We need to enumerate all such strings up to N=10. The alphabet is 26 letters, but the isomorphism class is about renaming letters: two strings s and t are isomorphic if there is a bijection (or possibly a mapping that preserves equality/difference) between the characters. The definition: For every pair i,j, one of the following holds: s_i = s_j and t_i = t_j, OR s_i != s_j and t_i != t_j. This is equivalent to the condition that the pattern of equalities is the same. Essentially, two strings are isomorphic if they have the same "pattern" when we map each distinct character to a symbol based on order of first appearance. For example, s = "abcac" pattern: 0,1,2,0,2 (assign numbers to new letters). t must have same pattern with some permutation of the letters. So isomorphic strings are exactly those that are obtained by applying a bijection from the set of distinct characters in s to some set of distinct characters (maybe not all of 26, but some subset) to get t. However, note that the alphabet is fixed (lowercase letters). For t to be a string over lowercase letters, we can apply any injective mapping from the distinct characters of s to the alphabet (since we don't need to be onto; it's okay to map to letters not used in s, but we only map the ones that appear). So the isomorphism class of s consists of all strings t such that t_i = f(s_i) for some injective function f: distinct(s) -> 'a'..'z'. Since f is injective, it preserves equality/difference. And any such t yields an isomorphic string. Also t must have length same as s, and the condition also holds for all pairs i,j; indeed, injective mapping ensures both directions. So isomorphism class = all relabelings of s via an injection.
Thus the normal form condition: s is lexicographically smallest among all relabelings via injective functions from its set of distinct letters to 26 letters. Since we can map s's letters to any letters (not necessarily the ones originally used), s is only in normal form if it is the lexicographically smallest string in its isomorphism class.
Given that we can relabel arbitrarily, the isomorphism class corresponds to a pattern: the first occurrence of each new character gets assigned a label in order of appearance. The lexicographically smallest string with a given pattern (over alphabet a..z) is the one where we assign the smallest possible letters to the pattern labels, i.e., the first distinct character gets 'a', the second gets 'b', etc. That is indeed the canonical representation. However, we must ensure that the resulting string is lexicographically smaller than any other relabeling. But is the canonical assignment always the lexicographically smallest? Consider pattern: say pattern = (0,1,0). The canonical string is "aba". Could there be another injection producing a string that is lexicographically smaller? For example, if we map 0->'a', 1->'a'? But injection cannot map two distinct source letters to the same target letter, so mapping must be injective. So we cannot map both 0 and 1 to 'a'. So the minimal assignment is to assign the smallest possible distinct letters to the pattern ids in order of first appearance. That yields the lexicographically smallest string among all injective relabelings? Let's test: pattern (0,1). The canonical is "ab". Another injection could map 0->'a',1->'c' giving "ac" which is > "ab". Or 0->'b',1->'a' gives "ba" which is > "ab"? Compare "ab" vs "ba": "ab" < "ba" because at first char, 'a' vs 'b', so yes. Could we get "aa"? Not allowed because injection requires 0 and 1 distinct. So indeed the canonical is smallest. But is it always? Suppose pattern (0,0,1). Canonical: "aab". Another injection: 0->'a',1->'b' yields "aab" (same). 0->'a',1->'c' yields "aac" which is larger. 0->'b',1->'a' yields "bba"? Actually mapping: 0->'b',1->'a' gives "bba"? Wait: pattern indices: first distinct letter (0) appears at positions 0,1; second distinct letter (1) at position 2. If we map 0->'b',1->'a', we get "bba"? That would be positions: 0:b,1:b,2:a => "bba". Compare "aab" vs "bba": at first char, 'a' < 'b', so "aab" is smaller. So canonical seems minimal.
But we need to be careful: The mapping could assign letters not in increasing order. But the lexicographic order depends on the entire string. The canonical assignment where the first distinct character maps to 'a', second to 'b', etc., yields a string that is minimal. Because suppose we have two different injective maps f and g. Compare the resulting strings s_f and s_g lexicographically. At the first position, the character is f(s_0) vs g(s_0). If s_0 corresponds to the first distinct character (id 0). In canonical, f(0)=a. For any other injection, g(0) could be a or something else. If g(0)=a, then we need to check further. If g(0)>a, then canonical is smaller. If g(0)=a, then at the first position they are equal. Now consider the smallest index i where s_i has a distinct id that is not mapped the same? We need to show that the canonical mapping yields the minimal possible string. This is a known fact: the lexicographically minimal string isomorphic to a pattern is obtained by labeling the first occurrence of each new letter with the smallest unused letter (starting from 'a'). That's the standard "isomorphic string reduction" used in problems like "Isomorphic strings" and "Smallest string from pattern". So indeed, the normal form strings are exactly those that are already in this canonical labeling: i.e., strings s such that if we replace each character by a label based on order of first appearance, we get the same string back? Wait, that's not exactly. The canonical labeling of s itself is the pattern: assign numbers. But the string s is in normal form if it is equal to the canonical labeling where we use letters 'a','b',... instead of numbers. That is, the string s must be such that the first occurrence of each distinct character appears in alphabetical order: the first distinct character must be 'a'; the second distinct character (the first character that is not 'a' and appears for the first time) must be 'b'; the third distinct character must be 'c'; and so on. Also, after that, each character must be consistent: if a character appears later, it should be the same as the one assigned earlier.
Thus the condition: For each position i, let the character s_i be some letter. Consider the mapping: the distinct letters in order of first appearance: the first letter (at index 0) is the first distinct; it must be 'a'. Then, the first occurrence of a letter different from all previous must be the next letter in alphabet: if we have used k distinct letters so far (say 'a'..'chr(k-1)'), then the next new letter must be the (k)th letter (starting from 0? Actually 'a' is 0, 'b' is 1, etc.). So the new letter must be exactly the letter corresponding to the count of distinct letters before it. In other words, the string s must be such that when scanning from left to right, each time we encounter a new character, it must be the smallest letter not yet used among 'a','b',... that hasn't been used. And all subsequent occurrences of that character must match.
Is that sufficient? Also need to ensure that s is lexicographically smallest among all isomorphic strings. If s satisfies that, then any injection f that differs from the identity mapping (where f maps each distinct char to itself) would produce a string that is either equal (if f is identity) or different. To be smaller, one would need at the first position where f differs, the mapped char < original. But original is 'a' at first position. Since f is injection, f('a') could be something else. Could f('a') be less than 'a'? No, because 'a' is the smallest letter. So f('a') must be >= 'a'. If f('a') = 'a', then we continue. Next, if the second distinct char is 'b', at its first occurrence, if f('b') is something else: it could be 'a'? But f('a') = 'a' already, injection prevents f('b') = 'a' because 'a' is already used. So f('b') must be >= 'b'? Actually could be 'c' etc. But could be 'b' if identity. If f('b') > 'b', then the string becomes larger. If f('b') = 'b', then continue. So identity yields the smallest. But what if the distinct letters are not in alphabetical order? For example, s = "baa". Distinct order: first distinct is 'b', then 'a'? Actually scanning: position 0: 'b' (new -> first distinct = 'b'). Position 1: 'a' (new -> second distinct = 'a'). But this order is not alphabetical: first distinct is 'b', second is 'a'. This string does not satisfy the condition we described. Would it be in normal form? Let's check isomorphism class of "baa". Pattern: (0,1,1) where 0 for 'b', 1 for 'a'. Canonical labeling: assign 0->'a', 1->'b' yields "abb". That is "abb". Compare "baa" vs "abb": "baa" > "abb"? Lexicographically: compare first char: 'b' vs 'a', so "baa" > "abb". So "baa" is not minimal; the minimal is "abb". So "baa" is not in normal form. So indeed the normal form strings are exactly those that are already canonical: they have the property that when we assign numbers based on first occurrence, the actual letters used correspond exactly to those numbers (i.e., the letter for label i is the i-th letter of the alphabet). That is equivalent to saying that the string is in "lexicographically minimal form" for its pattern. So we just need to generate all strings of length N over lowercase letters such that: if we define the set of distinct letters in order of appearance, they are exactly 'a', 'b', 'c', ... in that order. In other words, the string must be such that it does not "skip" a letter: the first distinct letter must be 'a'; if there is a second distinct letter, it must be 'b'; third distinct 'c'; ... up to the number of distinct letters used. Also, the string cannot use any letter that is not among these distinct ones? Actually it can only use the distinct ones, and they must appear in that order. So the allowed letters are exactly the first d letters of the alphabet for some d (d = number of distinct letters). And the string must be such that the first occurrence of each letter is in order: the first letter is always 'a'; the first time we see a letter different from 'a' must be 'b'; the first time we see a letter different from 'a' and 'b' must be 'c'; etc. Also, after a letter appears, it can reappear later any number of times.
But is that also necessary? For example, consider N=3, string "aba". Distinct order: first 'a', second 'b'. That matches: first distinct is 'a', second is 'b'. So it qualifies. And "abb" also qualifies? Distinct order: first 'a', second 'b' (since after 'a', first new letter is 'b' at index 1). So "abb" qualifies. "abc": distinct order: 'a','b','c' qualifies. "abac"? (N=4) distinct order: 'a','b','c' qualifies (first c appears at index 3). So yes.
Now, what about using more than 26 distinct letters? N <=10, so at most 10 distinct letters, which is fine.
So the problem reduces to: generate all strings s of length N over 'a'..'z' such that if we scan from left to right, each time we encounter a new character (not seen before), it must be exactly the smallest letter not yet used (i.e., if we have seen k distinct letters so far, the new letter must be the (k+1)-th letter of the alphabet, i.e., char('a'+k)). And all letters used must be among the first d letters (no other letters appear). Equivalent to: s is any string of length N that can be generated by the following process: start with an empty set of used letters; for each position i from 1 to N, choose a letter that is either one of the letters already used, or if you want to introduce a new letter, it must be the next smallest letter (i.e., the smallest letter not used yet). So the number of distinct letters d can range from 1 to min(N,26). For each d, we need to list all strings of length N using exactly d distinct letters (the first d letters 'a'..chr('a'+d-1)) such that the first occurrence of each letter is in increasing order: 'a' appears first (at position 0), the first occurrence of 'b' appears after the first 'a' and before the first 'c', etc. Actually the condition only requires that when we encounter a new letter, it is the next smallest. So that implies that the first occurrence of 'a' is at position 0; the first occurrence of 'b' can be at any position after some point, but it must be the first new letter after we have used only 'a' so far. So it must appear before any other new letter like 'c'? Actually if we have only used 'a' so far, the next new letter must be 'b'. So the first time we see a letter different from 'a', that letter must be 'b'. After we have used 'a' and 'b', the next new letter must be 'c'. So the order of first appearances is forced: 'a' then 'b' then 'c' ... sequentially. So the string's distinct letters are exactly the first d letters, and they appear first in that order. However, there is no constraint on relative ordering beyond the first occurrences? For example, could we have "aab" -> first new: a, then a (old), then b (new) -> ok. Could we have "aba" -> first a, then b (new) -> b is next new after a, so ok. Then later a (old) is allowed. So it's fine. Could we have "baa"? Not allowed because first letter is b, not a.
Thus, the normal form strings are exactly those that can be described as strings over alphabet where the set of distinct letters is a prefix of the alphabet, and the first occurrence of each letter is in order.
But is that condition also sufficient for s to be lexicographically smallest among its isomorphism class? Let's verify. Suppose s satisfies the condition. Then its pattern (first occurrence labels) is exactly: label for 'a' is 0, 'b' is 1, etc. The canonical relabeling would map 0->'a',1->'b',... yields exactly s. For any other injection f, we need to show s <= t where t_i = f(s_i). As argued earlier, because the mapping is injective and s uses the smallest possible letters for each label in order, any other injection will either map the first distinct letter to something > 'a' (if not identity) because injection forces f('a') cannot be something less than 'a'. Could f('a') be 'a'? Possibly. But if f('a') = 'a', then consider the first distinct label that maps to a letter different from its original. Suppose the first such label is j (i.e., for label i < j, f(letter i) = letter i; for label j, f(letter j) != letter j). Since f is injective and the previous i letters used are exactly the first i letters (because they are mapped identically), f(letter j) cannot be any of those i letters (since they are already used), and must be a letter not among the first i letters. The smallest such unused letter is the (i+1)-th letter, which is exactly letter j (since j = i+1? Actually labeling: distinct letters in order: a=0, b=1, c=2,... So j is the index, and letter j is the (j)-th letter (0-indexed). If we have used letters 0..j-1 for mapping identically, the available letters are all letters except those used. The smallest unused letter is indeed letter j (since letters are a..z and we've used a..(a+j-1)). So f(letter j) must be >= letter j+? Actually the smallest unused letter is letter j (because letters used are a... (a+j-1)). But letter j is exactly the original letter for label j. Since f(letter j) != letter j, it must be > letter j. So at the first position where s_i corresponds to label j, we have t_i = f(letter j) > letter j = s_i, so s < t. Thus s is strictly smaller. So condition is sufficient. So indeed normal form strings are exactly the strings that satisfy: when scanning from left to right, each time a character appears that hasn't appeared before, it must be the smallest letter not yet used. That's a clear constructive characterization.
Therefore, we can generate all such strings for N up to 10. The number of such strings? It's the number of strings of length N with the property that the first occurrence of each distinct letter is in order. This is a known combinatorial object: these strings correspond to set partitions with ordering? Actually it's like restricted growth strings (RGS) used in set partitions (aka restricted growth functions). Indeed, a restricted growth string (or restricted growth function) of length N is a sequence a_1 a_2 ... a_N such that a_1 = 0, and for i > 1, a_i <= 1 + max(a_1,...,a_{i-1}). And they are used to encode set partitions: each value corresponds to the block number of the element. Here, if we map each distinct letter to an index (starting from 0), then the condition that new letters appear in order (the first time we see a new letter, it's the smallest not used) is equivalent to saying that if we assign to each position the index of the distinct letter (0 for the first distinct, 1 for the second, etc.), the sequence is a restricted growth string with the additional property that the values used are exactly 0,1,...,d-1 for some d, and the first occurrence of each value appears in increasing order (which is automatically satisfied by the definition of RGS? In standard RGS, a_1 = 0, and whenever a new value appears, it is exactly max_so_far+1. So it's exactly the same. Indeed, our strings correspond to these RGS sequences, but with letters instead of numbers: we map 0->'a', 1->'b', etc. So the number of such strings of length N is the number of RGS of length N with values at most 25 (but N<=10 so always within 26). The number of RGS of length N is the Bell number? Actually RGS sequences for set partitions count set partitions: each RGS corresponds to a set partition of an N-element set, where the number of blocks is the number of distinct values. The number of such sequences is the Bell number B(N). Because each set partition can be represented uniquely by a restricted growth string (RGS). Indeed, B(N) = sum_{k=0..N} S(N,k) where S are Stirling numbers of second kind, and each RGS with exactly k distinct values corresponds to a partition into k blocks. So the number of strings we need to output for N is Bell(N). For N=10, Bell(10) = 115975? Let's compute: Bell numbers: B0=1, B1=1, B2=2, B3=5, B4=15, B5=52, B6=203, B7=877, B8=4140, B9=21147, B10=115975. Yes. So up to 115975 strings for N=10. That's feasible to generate and output, as N is small. But we need to output each string on a separate line. 115975 lines is okay (approx 115k lines, each string length 10). That's well within typical limits (maybe around 1MB). So we can just generate all such strings by DFS/backtracking using the rule.
Alternatively, we could generate all RGS sequences and map to letters. Since we need to output in lexicographic order of the strings (i.e., the actual strings over letters). Note: the RGS sequences when mapped to letters (0->a,1->b,...) produce strings that are sorted lexicographically? Not necessarily; we need to output the strings in lexicographic order. Our generation method must output them sorted. We can generate them in lexicographic order directly by exploring choices for each position from 'a' to 'z', but with the constraint that if we pick a new letter, it must be the smallest not used. That can be done by recursion: we maintain the set of used letters (or rather the next available new letter). At each position, we can either choose an already used letter (any of the ones used so far) or choose the next new letter (which is predetermined as the smallest not used). However, the used letters are always a prefix of the alphabet from 'a' up to some max. So the set of used letters is exactly {'a', 'b', ..., last_new} where last_new is the max letter introduced so far (say d distinct letters). Then the next new letter would be char('a' + d). So the choices for each position: either pick any letter from 'a' to last_new (inclusive) (the already used ones) OR pick the next new letter (if we haven't exceeded 26 and we want to introduce a new one) which is char('a' + d). The selection of any used letter includes all of them. However, note that picking a used letter may affect lexicographic ordering; we need to generate strings in lexicographic order. The natural recursion where we iterate over possible characters in increasing order (from 'a' to 'z') but with the constraint that we cannot pick a letter that is not allowed: if we are at a state with used letters set being a prefix, the allowed letters are: all letters from 'a' to last_new (already used) are allowed; also the next new letter (if available) is allowed; but any letter beyond that (i.e., > next new) are not allowed because that would skip a letter: e.g., if we have used 'a' and 'b', then next new must be 'c'. If we pick 'd' directly, that would be a new letter but not the smallest unused; that would violate condition. So such letters are not allowed. Also we cannot pick a letter that is less than 'a'? Obviously not. So the allowed set is exactly the prefix up to (last_new) plus possibly the next new letter if we choose to introduce it.
But careful: When we are at a state, we have a current maximum used letter: suppose we have used letters {a, b, c} => last_new = 'c' (d=3). The next new letter would be 'd'. Allowed letters: 'a', 'b', 'c', and also 'd' if we decide to use a new letter. But what about picking a letter that is not among these? e.g., 'e' not allowed because that would be a new letter but not the smallest unused; that violates condition. So only those.
Now, note that if we have d distinct letters, the set of used letters is exactly the first d letters. So the allowed letters are exactly the first d+1 letters? Actually if we have d distinct, we have used letters 0..d-1 (mapped to a..). At a position, we can either reuse one of the existing d letters (0..d-1) or introduce a new one, which must be exactly d (i.e., the d-th letter). So allowed letters: a..(a+d-1) and (a+d) if we allow new. So yes.
Thus we can generate all strings by recursion over positions, maintaining d (number of distinct letters used so far) and maybe an array for the next available new letter. Since N<=10, the recursion depth is small.
We must output strings in lexicographic order. If we recursively iterate over characters in increasing order (from 'a' upward), we need to consider that the set of allowed characters is not a simple range from 'a' to 'z'; it's a contiguous range from 'a' to (a+d) (where d is current distinct count). However, note that d can increase as we go deeper. At a given state, the allowed letters are exactly the first (d) letters plus maybe the (d+1)-th if we choose to expand. But these are contiguous: from 'a' to (a + d) inclusive? Wait, if d distinct used, the used letters are exactly a..(a+d-1). So the allowed letters for reuse are a..(a+d-1). And the allowed new letter is (a+d). So overall, allowed letters are a..(a+d) inclusive! Because (a+d) is the next new letter. So the allowed set is exactly the first d+1 letters (a..a+d). That's a contiguous range starting from 'a'. But is it always contiguous? Yes, because we never skip a letter: we always introduce the smallest unused letter, so the used letters are always a prefix. Thus the allowed letters at any state are exactly the first (d+1) letters, where d is the number of distinct letters already used. However, we must be careful: is it always allowed to use any of the first d letters? Yes. And using the (d)-th letter (which is already used) is allowed. And using the (d)-th? Actually the d-th distinct letter is at index d-1, so letters from 0 to d-1 are already used. The new one is index d. So allowed set is indices 0..d inclusive. So the allowed characters are contiguous from 'a' to (a+d). So recursion can simply iterate c from 'a' to (a+d) inclusive. But wait, consider state where we have d distinct, and we pick a character that is less than (a+d) but not among the used ones? But since the allowed set is contiguous from a to a+d, but if we pick a character that is less than a+d and is not used? But all characters from a to a+d-1 are used (since we have used exactly the first d letters). Actually are we guaranteed that all letters from a to a+d-1 have been used? Yes, because we introduced them in order: the first distinct is a; later we introduced b, then c, etc. So after introducing d distinct letters, we have definitely used all letters from a to a+d-1. However, could it happen that some of those letters have not been used? Suppose we have d=2, used letters: first distinct is a, second distinct is b (so b is used). But what about the letter 'a'? It's used. So both a and b are used. So indeed all letters from a to a+d-1 are present. So they are used. So any character from a to a+d-1 is an already used letter. So the allowed set is contiguous: a..a+d. So the recursion can simply iterate over characters from 'a' to (chr(ord('a') + d)), where d is current distinct count. But we need to update d accordingly: if we pick a character that is equal to the next new one (chr(ord('a')+d)), then we are introducing a new distinct letter, so new distinct count becomes d+1. Otherwise, if we pick any other character (which must be from a..a+d-1), the distinct count stays d.
Thus algorithm:
- Recursively build string s of length N.
- Keep track of current distinct count d (initially 0 before any character? Actually at start, we have no letters used. For first position, the allowed characters: we can pick any? But condition: first character must be the smallest not used, which is 'a'. Because it's the first character, and it's the first occurrence of any letter, it must be 'a'. So at position 0, the only allowed character is 'a'? Let's check: At start, no letters used. The set of used letters empty. The next new letter is 'a'. So we must pick the new letter because there is no used letter to reuse. So the only allowed choice is 'a'. So d=0 at start; allowed set should be a..a+0 = 'a' only. That matches. So recursion works if we set d=0 initially. At position i, allowed characters are from 'a' to (chr(ord('a')+d)). But d is the number of distinct letters used so far. At start d=0: allowed 'a' (only). So that's correct.
Now, after picking a character c, we need to update d: if c is exactly the next new letter (i.e., ord(c) - ord('a') == d), then we are adding a new distinct letter, so new d = d+1. Otherwise, new d remains d (since we used an existing letter). So the recursion is simple.
We need to generate all strings of length N with this rule. The number of such strings is B(N). That matches.
We also need to output them in lexicographic order. Since we are generating by trying characters in increasing order from 'a' up to (a+d), and we recursively fill positions, the DFS will generate strings in lexicographic order because we are exploring options for each position in ascending order. But we need to verify: The recursion order: at each position, we iterate c from 'a' to (a+d). This will produce strings sorted lexicographically? Consider two strings s and t. For them to be in lexicographic order, we compare first character. Since we iterate c from 'a' upwards, the strings with first character 'a' will be generated before those with 'b' etc. But is 'b' ever allowed as first character? No, because at first position d=0, allowed only 'a'. So all strings start with 'a'. That's fine. At second position: d might be 1 if we introduced new at first (which we did, since only 'a' was used, d becomes 1 after first char). So at second position, allowed characters are 'a'..(a+1) i.e., 'a' and 'b'. So we iterate c='a' then c='b'. This yields strings where second char is 'a' before those with 'b'. That's lexicographically correct: "aa..." < "ab...". But wait, what about strings where we later introduce new letters? For example, N=2: possible strings: "aa", "ab". Our recursion: first char 'a' (fixed). Then for second char, try 'a' -> "aa". Then try 'b' -> "ab". That's lex order: "aa", "ab". For N=3: we need to generate all B(3)=5 strings: "aaa", "aab", "aba", "abb", "abc". Let's see if recursion yields in lex order:
Start: pos=0, d=0 -> only 'a'. After that d=1.
pos=1: allowed 'a','b'.
- choose 'a': d stays 1. Then pos=2: allowed 'a','b'.
- choose 'a': "aaa".
- choose 'b': "aab".
- choose 'b': d becomes 2. Then pos=2: allowed 'a','b','c'? Wait d=2 -> allowed up to 'b'? Actually after picking 'b' at pos1, d becomes 2 because we added new distinct 'b' (since d was 1, new char 'b' is exactly the next new). So at pos2, d=2, allowed 'a','b','c'? But according to rule, allowed set is a..a+d = a..(a+2) = 'a','b','c'. But is that correct? Let's check state: we have used letters: 'a' and 'b'. The next new letter would be 'c'. At pos2, we can reuse any of 'a','b' or introduce new 'c'. So allowed are 'a','b','c'. That matches. So from that branch:
- choose 'a': "aba".
- choose 'b': "abb".
- choose 'c': "abc".
Now, ordering of these strings: "aaa", "aab", "aba", "abb", "abc". Is that lexicographically sorted? Compare "aab" and "aba": "aab" < "aba"? Yes because at second character: 'a' vs 'b'? Actually "aab": chars: a a b; "aba": a b a. Compare: position0: both 'a'; position1: 'a' vs 'b' => 'a' < 'b', so "aab" < "aba". So order is correct: "aab" before "aba". In our recursion order: after finishing the branch with first pos1='a', we output "aaa","aab". Then we go to branch pos1='b', output "aba","abb","abc". So overall order is "aaa","aab","aba","abb","abc". That's indeed lexicographic order because "aaa" < "aab" < "aba" < "abb" < "abc". So recursion with increasing char selection yields lexicographic order.
But we must confirm for longer strings if that always holds. Since we always explore the smaller character first, and we fix the prefix before exploring later prefixes, the resulting order is exactly the lexicographic order (dictionary order) of strings. So we can just do DFS and output each complete string.
Edge Cases: N up to 10, alphabet size 26, but we might attempt to use more than 26 distinct letters? For N=10, maximum distinct is 10, so never exceed 26. So it's safe.
Thus solution: for N given, generate all strings via DFS and print.
But we should confirm that this characterization indeed covers all normal form strings. We already reasoned that normal form string must be lexicographically minimal in its isomorphism class, which is equivalent to being canonical labeling. That yields the property. But is there any nuance: the definition of isomorphism requires the mapping to be a bijection? Actually they said: For every pair i,j, conditions hold. That is exactly the condition for there to exist a bijection between the sets of characters? It's essentially the same as saying there is an injective function f from the set of characters in s to characters (since mapping must be injective to preserve inequality) and also surjective? Not necessarily surjective onto the alphabet, but between s and t we need a bijection between the sets of characters that appear? Actually if s has distinct letters set A, t has distinct letters set B, the condition implies there exists a bijection between A and B. Because for each distinct char in s, we can map to the corresponding char in t (if we have multiple occurrences, it must be consistent). The definition with pairs ensures that the relation "equal in s iff equal in t" holds. This is equivalent to saying the pattern of equalities is the same. That implies there is a bijection between the set of distinct characters in s and the set in t (if both have the same number of distinct characters). But note that the condition does not require the mapping to be onto all letters of t? Actually if t has extra distinct characters that are not in the range? But s and t have the same length; the condition: For every pair i,j: s_i = s_j <=> t_i = t_j. That forces that the number of distinct characters in s equals the number in t. So the sets have same size, so a bijection exists. So isomorphic strings have the same pattern and same number of distinct characters. So the class is defined by any relabeling using a bijection between the distinct sets. But if we only consider injection from s's distinct set to alphabet, that yields a t whose distinct set is the image; but the image may be a subset of alphabet, but the mapping from s to t is bijective onto that image. So it's okay. So the class is exactly all strings obtained by applying a bijection from s's distinct set to some set of size equal (any subset of alphabet). So the minimal lexicographic string among them is indeed the one where we map to the smallest letters in order: assign 'a' to the first occurring distinct character, 'b' to the second, etc.
Thus our characterization stands.
Now we just need to implement. Since N <= 10, we can use recursion or iterative generation.
Plan:
- Define recursive function dfs(pos, d, current_string):
if pos == N: print current_string
else:
for c in range from 'a' to chr(ord('a')+d):
next_d = d
if ord(c) - ord('a') == d: # c is the next new letter
next_d = d + 1
# else: next_d = d
dfs(pos+1, next_d, current_string + c)
But careful: when d = current number of distinct letters used so far. At a position, allowed letters are 'a' to chr(ord('a')+d). That includes both existing and the next new. We'll iterate over c_char = chr(ord('a') + i) for i in range(d+1). For each, if i == d, then it's new, so next_d = d+1; else next_d = d.
This will generate all strings in lex order.
But is there any need to consider that after using a new letter, the next new letter becomes the next smallest, which is automatically covered because d increases.
Edge Cases: N=0? Not possible because N>=1. So ok.
Complexities: For N=10, the number of strings is B(10)=115975, and recursion depth 10, fine.
Now, we should test with given examples:
N=1: Only string: "a". Our recursion: pos0,d=0, allowed i=0 only (c='a'), next_d=1, pos==1 print "a". So output "a". Good.
N=2: Strings: "aa","ab". Output as example.
N=3: We listed "aaa","aab","aba","abb","abc". Check if these are all? B(3)=5, yes.
N=4: B(4)=15. Let's manually list? Not needed.
We need to ensure that we don't miss any strings that might be in normal form but have more than 26 distinct letters, but N<=10, so fine.
Thus solution is straightforward.
But wait, is there any other nuance: The definition of normal form: For every string t that is isomorphic to s, s <= t holds. Could there be a scenario where the canonical labeling (assigning a,b,c in order) is not unique? Suppose s uses letters that are not in alphabetical order of first occurrence, but still s is lexicographically smallest among its class? Let's test: s = "ab". Canonical labeling yields "ab". Is there any other string t isomorphic to "ab"? t must have two distinct letters, pattern (0,1). The possible t are any two distinct letters, e.g., "ba", "ac", "bc", etc. Among these, lexicographically smallest is "ab" because we can map 0->'a', 1->'b' yields "ab". Could there be a string like "aa"? No, because pattern must have two distinct characters. So "ab" is minimal. Could there be a string like "ba"? That is larger. So "ab" is normal form. That matches our property.
Now consider N=2, s = "aa". Is it normal? pattern (0,0). Possible t? Any string with a single letter: "a", but length must be same N=2? Actually t must have length 2 and pattern all same. So t must be two same letters, e.g., "bb","cc",... The smallest lexicographically among those is "aa"? Compare "aa" vs "bb": "aa" < "bb". So yes, "aa" is normal. That matches our property because first char 'a' and no new letter.
Now consider s = "aba". As we said, pattern (0,1,0). The class includes strings like "bab"? Actually map 0->'b',1->'a' gives "bab". Compare "aba" vs "bab": "aba" < "bab". So normal? Yes. And our condition says s must have first distinct 'a', second distinct 'b', which holds: scanning: a (new->a), b (new->b because next new), a (old) -> ok. So it's included.
Now consider s = "abac". Pattern (0,1,0,2). Our condition: first a, then b, then a, then c (new -> c) because next new after a,b is c. So it qualifies.
Now consider s = "abca". Pattern (0,1,2,0). Condition: first a, then b (new), then c (new), then a (old). Qualifies.
Now consider s = "abcb". Pattern (0,1,2,1). Condition: first a, then b (new), then c (new), then b (old). Qualifies.
Now, are there any strings that satisfy condition but not lexicographically minimal? Let's test a borderline: Suppose we have s = "aab". Condition satisfied? First a, second a (old), third b (new). That's fine. Is it minimal? Pattern (0,0,1). Other relabelings: map 0->'a',1->'c' gives "aac", which is > "aab". Map 0->'b',1->'a' gives "bba"? Actually careful: 0->'b',1->'a' yields "bba"? Because positions: 0:b,1:b,2:a => "bba". "aab" < "bba". Map 0->'a',1->'b' is identity. Could there be a mapping that yields a string smaller? To be smaller, at first position we need something < 'a', impossible. So yes.
Now, suppose we have s = "abc". Condition satisfied. Minimal.
Thus the characterization seems correct.
So we just implement DFS.
But the problem might have nuance: The strings are over lowercase English letters (26). Could there be normal form strings that use letters beyond the first d letters? For example, s = "acb"? Scanning: first a (new->a), second c (new but the smallest unused after a is b, not c; so condition fails because we introduced c before b). So not normal. Could it still be minimal? Let's test: pattern (0,1,2). The class includes all strings with three distinct letters in some order. The canonical minimal is "abc". "acb" > "abc"? Compare "abc" vs "acb": at position 0 both a, position1: b vs c, b<c, so "abc" < "acb". So "acb" is not minimal. So not normal.
What about "abacab"? That's pattern (0,1,0,2,0,1). Condition: first a, b new, a old, c new, a old, b old. It qualifies. So good.
Now, is it possible that the lexicographically smallest string in the isomorphism class is not the one with the canonical assignment? Consider pattern where we have distinct letters in some order. The minimal lexicographic string under the constraint that distinct letters are mapped injectively to letters, where the mapping can be any injection. The lexicographic minimization problem: we have pattern with labels 0..k-1 (in order of first appearance). We need to assign distinct letters to these labels to minimize the resulting string lexicographically. Since the first character is label at position 0 (call it label L0). To minimize, we want the letter assigned to L0 to be as small as possible. The smallest possible letter is 'a'. So assign f(L0) = 'a'. Then among remaining labels, consider the next position where the label is not L0. The assignment to that label should be as small as possible, but must be distinct from 'a'. So the smallest is 'b'. And so on. So indeed the greedy assignment yields the canonical string. So our characterization is correct.
Therefore, answer: generate all strings length N that are in normal form. That's all restricted growth strings mapped to letters.
We must output them in lexicographic order. Our DFS generates in lex order if we iterate characters in increasing order. Good.
Now implement solve():
- Read integer N.
- Use recursion with string builder (list) to avoid copying strings frequently? Since N is small, copying is fine.
- Use iterative approach? Could also generate all RGS numbers and then convert to letters. But recursion is simpler.
Implementation details:
- Use Python recursion (depth <=10, safe).
- Instead of ord calculations each time, we can maintain integer d (number of distinct letters used). The allowed characters indices: 0..d inclusive. Convert to char by chr(ord('a')+i).
- Or maintain next_new = d, and existing letters are indices 0..d-1.
- For each i in range(d+1):
next_d = d+1 if i == d else d
dfs(pos+1, next_d, s+chr(ord('a')+i))
But careful: The state d is number of distinct letters used so far. After choosing character with index i, if i == d (meaning we chose the next new letter), then new distinct count becomes d+1. Else, remains d.
But one nuance: When we choose an existing letter (i < d), is it guaranteed that this letter has been used before? Yes, because i corresponds to a letter that is already in the set (since we have used all letters from 0 to d-1). However, could there be a scenario where we have used letters but not all of them? In our construction, we always maintain that we have used exactly the letters 0..d-1. Because when we introduce a new letter, we always introduce the smallest unused, which is d. So the set of used letters is always exactly the set {0,1,...,d-1}. So any i < d is indeed an existing letter. So it's fine.
Thus the algorithm ensures the set of used letters is always a contiguous prefix.
Now, complexity: number of recursive calls equals number of strings times average branching? But we can just generate all strings.
We must ensure to output each string on a new line. For large N=10, 115975 lines, it's okay.
We should test with N=2 and N=1 to verify.
Now, let's consider if there might be any subtlety about the definition of normal form: "For every string t that is isomorphic to s, s <= t holds." This means s must be the smallest among all isomorphic strings. If there are multiple strings with same pattern, the canonical one is smallest. So our characterization is correct.
But wait: Could there be a string that is lexicographically smallest but does not have the property that first distinct is 'a', second distinct is 'b', etc.? Suppose the pattern has labels, and we assign f(0)='a', f(1)='b', ... that yields a string S. Could there be another assignment that yields a lexicographically smaller string than S? No, as argued.
But could there be a scenario where the pattern has less than full use of letters? That's already covered.
So we are done.
But let's double-check with N=0? Not needed.
Edge Cases: N=10, maximum d is 10, so we never go beyond 'j'? Actually 10 letters: a..j. So no issue with alphabet size.
Now, implementation in Python:
def solve():
import sys
sys.setrecursionlimit(1000000)
N = int(sys.stdin.readline().strip())
out_lines = []
# Use recursion with list for efficiency
def dfs(pos, d, cur):
if pos == N:
out_lines.append(''.join(cur))
return
# allowed letters: indices 0..d inclusive
for i in range(d+1):
cur.append(chr(ord('a')+i))
if i == d:
dfs(pos+1, d+1, cur)
else:
dfs(pos+1, d, cur)
cur.pop()
dfs(0, 0, [])
sys.stdout.write('\n'.join(out_lines))
But note: At start, d=0, allowed i in range(1) -> i=0 only. That's correct.
Now test for N=3: should get 5 strings. Let's simulate quickly.
Check order: For N=3, recursion:
dfs(0,0,[]) -> i=0: cur=['a']; call dfs(1,1,['a'])
- dfs(1,1,['a']): allowed i=0,1
i=0: cur=['a','a']; call dfs(2,1,['a','a'])
dfs(2,1,['a','a']): allowed i=0,1
i=0: cur=['a','a','a']; pos=3 -> output "aaa"
i=1: cur=['a','a','b']; output "aab"
i=1: cur=['a','b']; call dfs(2,2,['a','b'])
dfs(2,2,['a','b']): allowed i=0,1,2
i=0: cur=['a','b','a']; output "aba"
i=1: cur=['a','b','b']; output "abb"
i=2: cur=['a','b','c']; output "abc"
Thus output order: aaa, aab, aba, abb, abc. That's lex order? Compare aab and aba: aab < aba, yes. So order is correct.
Now, for N=4, Bell number 15, we can list quickly to ensure no missing. But trust.
Thus solution done.
However, we need to confirm that the definition of normal form does not require that the string is minimal among all strings that are isomorphic to it, but we must consider all t isomorphic to s. Our reasoning shows that the minimal string in an isomorphism class is exactly the canonical one. So the set of normal form strings is exactly the set of canonical strings. That's correct.
One more nuance: Could there be two different strings that are both canonical for the same pattern? No, because canonical assignment is unique: for a given pattern, the mapping from labels to letters is fixed: label 0->'a', 1->'b', ... So the canonical string is uniquely determined by the pattern. Thus each isomorphism class corresponds to exactly one normal form string. And each normal form string corresponds to exactly one isomorphism class. So the number of normal form strings equals the number of isomorphism classes of strings of length N, which is the number of patterns, which is the number of restricted growth strings of length N, i.e., Bell(N). So the problem is essentially enumerating all RGS of length N and converting to letters.
Thus the answer.
Now, is there any other potential interpretation? The definition of isomorphism: They require for every pair i,j, one of the following holds: s_i = s_j and t_i = t_j, or s_i != s_j and t_i != t_j. That's equivalent to saying the equality pattern is the same. So our interpretation is fine.
We must also ensure that we consider only strings over lowercase English letters. Our generation only uses letters 'a'..'j' etc., which are fine.
Now let's test N=2 manually: Output should be "aa" and "ab". Our code will produce that.
Now test N=4: Let's compute expected count: B(4)=15. We can quickly generate mentally? Not needed.
Now check if there are any strings with d=0? Not possible because length >=1, so at least one distinct letter, so first char always 'a'.
Now we can implement.
But we must consider that output may be large for N=10 (115975 lines). In Python, printing line by line may be okay. But we can accumulate in list and join. Memory: each string length up to 10, 115975 strings -> ~1.16 million characters plus newlines ~115k lines -> about 1.3 MB, fine.
We must also ensure recursion depth is okay; depth=10, fine.
Alternatively, we could generate iteratively using stack. But recursion is fine.
Thus solution complete.
But wait, the problem statement says "Print all strings of length N that are in normal form, in lexicographically ascending order." They gave examples.
Now, is there any chance that the normal form definition includes strings that are not of the canonical form due to some tie-breaking? No.
Let's confirm by reading the definition: "For every string t that is isomorphic to s, s ≤ t holds." That means s is the minimum element in its isomorphism class under lexicographic order. For a given class, the minimum is indeed the canonical string. So all good.
Thus solution: generate all strings of length N satisfying: the first occurrence of each letter is in alphabetical order and only letters from 'a' upward are used without skipping.
We can also think of it as all strings that are lexicographically minimal among their "pattern" equivalence. There is known concept: "normal form" as defined.
Now I'll produce final answer.
Potential pitfalls: The recursion order we defined yields the strings in lexicographic order? Let's double-check with a more complex case to be sure. Consider N=4. Let's enumerate some strings and see if the order matches lex order. The lex order is standard dictionary order over strings of length N. Since we iterate characters from smallest to largest at each position, and we explore deeper positions after fixing a character, the DFS produces strings in lex order (depth-first, left-to-right). That is indeed standard. So output will be sorted.
Now, but note: The allowed set for each position is not the full alphabet, but a subset that is always a prefix of the alphabet. Since we iterate in increasing order over that prefix, we are still exploring smaller characters before larger ones, which preserves lex order.
Thus correct.
Edge Cases: N=10, maximum distinct letters =10, allowed characters up to 'j' (since 'a'+9 = 'j', and if we add new letter at last position, could be 'k'? Actually if we have d=9 at some point, allowed includes up to index 9, which is 'j', plus new index 10 = 'k'? Let's compute: 0->a, 1->b, ..., 9->j, 10->k. So we may use up to 'k' for N=10? Wait distinct letters can be up to 10, so indices 0..9 maximum. That's 'a'..'j'? Actually if we have 10 distinct letters, indices 0..9, the last distinct letter is index 9 which corresponds to 'j'? But index 0 is a, index 9 is j? Let's check: a=0, b=1, c=2, d=3, e=4, f=5, g=6, h=7, i=8, j=9. So 10 distinct letters: a through j. That's 10 letters: a,b,c,d,e,f,g,h,i,j. So max letter is 'j'. But wait, if we have 10 distinct, we need indices 0..9, so the highest letter is 'j'. But what about when we have d=9 distinct, allowed set includes up to index 9 (i.e., 'j')? Actually when we have d distinct letters, the next new letter index is d. So if currently d=9 (meaning we have used 9 distinct letters: indices 0..8), the next new letter index is 9, which is 'j'. So after adding it, d becomes 10 (distinct count 10), and then allowed set includes up to index 10? But with d