We need to solve: Given set of strings S (distinct, total length ≤ 1e5, n ≤ 1e5). Find a non-empty string T (over lowercase letters) such that each string s in S is a most frequent substring of T. That is, for each s ∈ S, let occ_T(s) be the number of occurrences of s in T. For any substring x of T (any string over alphabet that appears as substring), we require occ_T(s) >= occ_T(x). So the maximum frequency among substrings is achieved by each s ∈ S (they all must be among the substrings that have the highest frequency). In other words, the set of substrings with maximum frequency must include all S, and there may be other substrings with the same frequency (but that's allowed; they just need to be most frequent as well). However the condition is: "if the number of its occurrences is not less than number of occurrences of any other substring." That means s is a most frequent substring if its occurrence count is at least that of any other substring. So we need that each s ∈ S has occ_T(s) = M, where M is the maximum occurrence count among all substrings of T. (It could be that M is higher than occ_T(s)? But then s wouldn't be most frequent because there is a substring with higher count. So must have occ_T(s) = M, and M is the maximum.)
Thus we need to construct T such that all given strings appear as substrings, and they all have the same maximum frequency, and no other substring has strictly higher frequency. However, note that any substring appearing with frequency M is also "most frequent". So it's fine if other substrings also have same frequency M; but they would also be most frequent. The condition only requires that each given s is most frequent, i.e., its frequency is at least that of any other substring. So it's okay if there are other substrings with the same frequency; but we must ensure that no substring has frequency > M.
We want T with minimum length, and among those lexicographically smallest.
First, consider constraints: total length of strings ≤ 1e5, n up to 1e5. So each string is short on average.
Observation: For a string T, the maximum frequency substring often is a single character? But not necessarily. The problem imposes that each given s must have as many occurrences as any other substring. This is a strong condition.
Let's think: For any string T, consider the substrings and their frequencies. Typically, the single-character substrings may have high frequencies. If T has length L, then each character appears some number of times; let max frequency of a character be f_char_max. For any longer substring, its frequency is at most f_char_max? Not necessarily: For example, T = "aaaa", then single 'a' appears 4 times, "aa" appears 3 times, "aaa" 2 times, "aaaa" 1 time. So the maximum frequency is 4 (for 'a'). For T = "ababa", substrings: 'a' appears 3, 'b' appears 2, "aba" appears 2? Actually occurrences of "aba": at positions 1 and 3, so 2. The maximum is 3 (character 'a'). So often a single character has the highest frequency. But there are strings where a longer substring can tie or even exceed? Can a substring have more occurrences than any single character? Possibly if the string has many repeats of a pattern. For instance, T = "ababab", length 6. Character 'a' appears 3, 'b' appears 3. Substring "ab" appears? at positions 1,3,5 => 3. "ba" appears 2? Actually "ba" at 2,4 => 2. So max is 3. So tied. Could there be a substring with frequency > every character? Suppose T = "abcabcabc", length 9. Characters: 'a' appears 3, 'b' appears 3, 'c' appears 3. Substring "abc" appears 3. So max is 3. Could there be a case where a substring appears more times than each single character? Consider a string where each character is unique except for a repeating pattern that is longer than 1, but then each character still appears less often? For instance, T = "ab" repeated many times. Then each character appears L/2 times. Pattern "ab" appears L/2 times as well. So equal. To have a longer substring > any character, we would need that the pattern includes characters that are not all the same, and the characters individually appear less frequently than the pattern. But the pattern is composed of characters, each occurrence of pattern includes each character. The number of occurrences of a character is at least the number of occurrences of any substring that contains that character? Actually, consider substring x of length > 1. Each occurrence of x contributes to one occurrence of each character in x. So if x occurs f times, then each character in x occurs at least f times (maybe more because they can appear outside x). So we have occ(char) >= occ(x). Therefore for any substring x, the frequency of x is at most the minimum frequency among its constituent characters? Wait, careful: For a character c, each occurrence of x contributes an occurrence of c at that position, but c may also appear elsewhere. So occ(c) >= occ(x). Actually, if x occurs f times, then c appears at least f times (at those positions). So yes, occ(c) >= occ(x). So the maximum frequency among all substrings is always achieved by some single character? Not necessarily: because we consider substring of length 0? Not allowed. So the maximum frequency is equal to the maximum frequency of a single character in T. Because for any substring, its frequency is ≤ the minimum frequency of its characters? Let's be precise: Let x be a substring of length Lx. For each character c in alphabet, let occ(c) be number of occurrences of that character in T. For any occurrence of x, it uses specific positions that contain each character. Thus, if x occurs f times, then for each character c that appears in x, each occurrence of x contains exactly one instance of c (since x is a string; but it could contain multiple copies of c). However, each occurrence of x occupies distinct positions. So the set of positions where x occurs are a subset of positions where the first character of x occurs, but also must match entire string. But regardless, for any character c, the number of occurrences of c in T is at least the number of occurrences of x that contain that character in any given position. But careful: If x contains multiple instances of the same character (e.g., x = "aa"), then each occurrence of x uses two 'a's. So occ('a') >= 2 * occ(x) maybe? Actually, if x = "aa", each occurrence uses two 'a's, and these 'a's are distinct positions. So the total number of 'a's must be at least 2 * occ(x) (since occurrences could overlap? Overlap: in "aaaa", occurrences of "aa" at positions 1,2,3: they overlap; then the same 'a' positions are used multiple times? But occ('a') counts each position once. Overlapping occurrences share positions. So if T = "aaaa", occ('a')=4, occ("aa")=3. Here 4 >= 3, but not >= 2*3. So more complex.
Nevertheless, we can argue: For any substring x, the number of occurrences of x is at most the number of occurrences of its first character (or any character) because each occurrence of x starts at a position where that character occurs. Actually, each occurrence of x corresponds to a start position i such that T[i..i+|x|-1] = x. That start position must be a position where the first character of x occurs. But not every position where first character occurs yields an occurrence. So the set of start positions is a subset of positions where the first character appears. So occ(x) ≤ occ(first character of x). Similarly, occ(x) ≤ occ(last character) because end positions? Not exactly: For overlapping, start positions are distinct, so they are a subset of positions where first character appears. So occ(x) ≤ occ(c1) where c1 is first char of x. So indeed, for any substring x, its frequency is at most the frequency of its first character. Therefore, the maximum frequency among substrings is exactly the maximum frequency among characters (single-letter substrings). Because the single-character substrings are also substrings, so max_{substrings} >= max_{chars}. And for any x, occ(x) ≤ occ(first char) ≤ max_{chars}. So max_{substrings} = max_{chars}. So the most frequent substrings are exactly those substrings whose occurrence count equals the maximum character frequency, and also they must satisfy that their first character occurs exactly that many times (and perhaps also the entire string properties). In particular, any character that attains the maximum frequency is a most frequent substring. Also, any longer substring that also has that same frequency must start only at positions where the first character occurs (and those positions must all be starts of x) and also the first character must appear exactly that many times (so no extra occurrences of the first character that are not starts of x). Because if the first character appears more times than occ(x), then occ(x) < occ(first char) and thus cannot equal the maximum if first char is a max char. But if the maximum frequency is M, there may be characters with frequency M (call them max chars). For any substring x to have frequency M, its first character must have frequency M, and all M occurrences of that first character must be start positions of x (i.e., x occurs exactly at all positions where its first character appears). Similarly, the last character? Not necessarily, but consider constraints from overlapping? Actually, if first character appears M times, and x occurs M times, then x must start at each of those M positions. So x's occurrences cover exactly those positions. So the substring x is forced to be the string that appears starting at each occurrence of its first character. This imposes that at all positions where that first character occurs, the following characters must match x's subsequent characters. That means that the whole string T must have a periodic pattern: if we list the positions where first character occurs, say indices i1, i2, ..., iM, then for each such index, T[i_j ... i_j+|x|-1] = x. This implies that T is composed of repeating blocks? Not exactly, but it forces that the substring starting at those positions are identical.
Moreover, for x to be a substring with frequency M, it must also appear at positions that are not necessarily all occurrences of its first character? But to reach M, given that first character appears at most M times, we need that all occurrences of first character are start positions of x. So that is necessary.
Now, we are given a set of strings S. They all must be most frequent substrings in T, i.e., each s ∈ S must have frequency M (the maximum character frequency). So for each s, its first character must be a character with frequency M, and s must start at every occurrence of its first character. This already imposes strong consistency constraints.
Let M be the maximum character frequency in T. Let C be the set of characters that appear exactly M times in T. For each s ∈ S, we have first character s[0] must be in C, and s must occur at each position where that character appears. So for each character c in C, if there exists some s ∈ S with first character c, then that s must be such that at all positions where c occurs, the substring of length |s| starting there is exactly s. That means that for positions of c, the following characters are determined. If there are multiple s with same first character, they must be compatible: they must be such that one is a prefix of the other? Because if we have two different strings s and t both starting with same character c and both must occur at every occurrence of c, then at each occurrence of c, the substring starting there must equal both s and t, which forces s = t. So for each character c, at most one s ∈ S can have first character c. Moreover, if there is an s with first character c, then the string s defines what the T looks like at positions starting at each c. This essentially defines T as being built from a repeated pattern? Let's think.
Suppose we have a character c that appears M times. The positions are i1 < i2 < ... < iM. For a given s (length L), we require that for each k, T[i_k .. i_k+L-1] = s. This means that the segments starting at each i_k are identical. Overlaps may occur if gaps between positions are less than L. But still, the condition is that these segments must be consistent.
Now, consider two different characters c and d that both appear M times. They each might have associated strings s_c and s_d (maybe none). But note that M is the maximum frequency. The characters themselves c and d are most frequent substrings, which is okay. But if both c and d appear M times, they are both in C. For s_c and s_d, they impose constraints on T. Also, we must ensure that no substring has frequency > M, which is automatically satisfied because max char frequency is M.
Thus the problem reduces to constructing T (non-empty) of minimum length, such that for each s ∈ S, there is a character c = s[0] that appears exactly M times in T (where M = max character frequency), and for each occurrence of c, the substring of length |s| starting there equals s. Also, for any character that appears M times, it might not have an associated s; that's okay, but then that character itself is a most frequent substring, which is allowed; but it does not violate condition because we only require that given strings are most frequent; other substrings can also be most frequent. So having extra max-frequency characters is okay. However, we need to ensure that there is no substring with frequency > M, but we already argued that maximum substring frequency equals max character frequency, so it's safe.
But wait: Could there be a substring that is not starting with a max-frequency character but still has frequency M? Possibly if its first character appears M times but also some other characters appear M times? If its first character appears M times, then it can have frequency M only if it starts at all those M positions. So it's consistent.
Now, the main constraints are: For each s in S, let c = s[0]. Then c must appear exactly M times, and at each occurrence of c, the substring s must appear. Additionally, if there are multiple s with same first character, they must be identical, else impossible.
Also, there may be s with first character c that appears M times, but we also have other characters that appear M times; they might impose constraints that could conflict with the ones from s? Possibly if the segments overlap? For example, suppose we have c='a', s="ab", and also d='b' with some t? But t would start with 'b'. The occurrences of 'b' may overlap with the s occurrences. Since both c and d appear M times, there are M positions for 'a' and M positions for 'b'. The strings s and t impose conditions on T at positions where 'a' occurs and where 'b' occurs. These conditions must be mutually consistent. Moreover, the positions of 'a' and 'b' must be such that the overall string T of some length L respects these. We need to find minimal L string T satisfying all.
Let's formalize.
Let the alphabet be lowercase letters. We have S = {s1, ..., sn}.
We need to find T (string) and integer M = max_{char} freq_T(char) such that:
- For each s in S:
- freq_T(s) = M.
- Let c = s[0]; then freq_T(c) = M, and for every index i where T[i] = c, we have T[i..i+|s|-1] = s.
- Also, no character appears more than M times (by definition M is max). So any character that appears M times is allowed.
Additionally, T must be non-empty.
Observation: The condition freq_T(s) = M and s appears at each occurrence of its first character forces that |s| cannot exceed the distance between consecutive occurrences of c? Not necessarily; if the occurrences are close, overlapping is allowed. But the string must be consistent: If two occurrences of c overlap in the sense that the segment from one overlaps with the segment from another, then overlapping regions must match both s constraints, which may impose periodicity.
Consider the positions of c: indices i1,...,iM. For each k, we require that for j from 0 to |s|-1, T[i_k + j] = s[j]. This determines T at positions i_k + j, for all k and j. For different k, these assignments might conflict if they assign different values to the same position. That would be inconsistent -> no T.
Thus, we need to choose positions i1,...,iM (distinct indices in T) and the full T string such that these constraints are satisfied, and the maximum character frequency is exactly M (i.e., no character appears more than M times). However, note that characters other than those in S's first letters could appear arbitrarily many times but must not exceed M. So M must be at least the required frequencies of those characters. But we can choose M to be as small as possible to minimize length? Actually, the length of T will be influenced by M and the arrangement of positions.
We want T with minimum length. Since the constraints force many positions to be determined, we need to arrange the occurrences of these characters to minimize total length while satisfying constraints and ensuring no character appears more than M times. Also, we need to ensure that the set of positions where a given character c appears are exactly the start positions of s (for each s). For characters not in first letters of any s, they may appear any number of times ≤ M, but we can choose to not include them to keep length small. But they could be needed to fill gaps? Possibly we need to insert characters to make the string consistent? For overlapping constraints, we might need to define characters at many positions. The minimal length T is essentially the minimal string that can accommodate all required occurrences of the strings s at the positions of their first characters, with the property that each such first character appears exactly M times (and that M is the maximum frequency). Additionally, we must ensure that no character appears more than M times, which is automatically satisfied if we don't exceed. But we also need that for each s, the occurrences of its first character are exactly M; so we must have exactly M occurrences of each such first character across T. However, there could be multiple s with different first characters. That means we need to place exactly M occurrences of each of those characters. And those occurrences must satisfy the substring constraints. Also, we might need to place other characters (for the rest of the s's) which are determined by the s strings.
The string T is built from positions 1..L. The constraints essentially define a set of equations on characters at each position. We need to find a string of minimal length that satisfies all equations, and also that each specified character (first letter of each s) occurs exactly M times, and no other character occurs > M (but we can check later). Additionally, we need M to be consistent: it must equal the frequency of those first characters (since they are required to be M). But it's possible that some other character (like one that appears in the middle of some s) could also appear M times accidentally. That would still be okay as long as it doesn't exceed M. But could it exceed M? That would violate M being max. So we must ensure that no character appears more than M times. This includes characters that are part of the s strings. Since we are constructing T to satisfy constraints, we need to check that the counts of all characters are ≤ M. However, if we set M to be the maximum frequency of any character, it's automatically the max. So we need to ensure that among all characters, the highest count equals M. But we are forcing some characters to have count exactly M (the first letters of s). So M will be at least that. The other characters should have counts ≤ M. If any other character ends up with count > M, then the max would be > M, contradiction because then those s wouldn't be most frequent (their frequency M would be less than max). But could that happen? Yes, if the forced occurrences cause a character to appear many times. For example, if we have s = "aa" with first character 'a', then we need M occurrences of 'a' as starts. But s itself contains 'a' at position start and also second character 'a'. So each occurrence of s contributes an extra 'a' at the second position. So total count of 'a' would be at least 2M (from these occurrences if they don't overlap? But they might overlap; overlapping could reduce total distinct positions). Actually, careful: The positions determined by the occurrences of s: For each start position i (where T[i] = 'a'), we set T[i] = 'a' (already) and T[i+1] = 'a'. If the starts are spaced such that the second position of one occurrence coincides with the first position of another occurrence (overlap), then the same position can serve both. So the total number of distinct positions where 'a' occurs could be less than 2M. In fact, the maximum possible count of a character could be less than M? But we need to ensure it doesn't exceed M. So we must design the placement of starts (positions of the first characters) such that the total count of each character does not exceed M, while still having the first characters appear exactly M times. This is a nontrivial combinatorial packing problem.
Moreover, we need minimal length T. The length L will be determined by the span of positions from the first start to the last end of some s, plus possibly extra characters before or after if needed? But we can place the occurrences tightly; the minimal length will be the minimal possible L such that we can place M occurrences of each required first character, respecting substring constraints and character count bounds.
Given that total length of input strings is up to 1e5 and n up to 1e5, but M could be large? We need to find T with minimal length. However, note that T length can't be arbitrarily large because we are minimizing length, so we want as compact as possible.
We need to derive necessary conditions for existence and then construct minimal T.
Observation: For a given s, if we have M occurrences of its first character c, and for each occurrence we place the full s, then the set of positions covered by these occurrences is: For each start i, positions i...i+|s|-1. The union of these intervals determines the characters. Overlaps can reduce total distinct positions. To minimize total length, we would want to pack these intervals as tightly as possible, i.e., with maximum overlap, subject to consistency and other s's constraints.
If we only have one s, what is the minimal T? Let's analyze single s case.
Problem: Given a string s, find a non-empty string T of minimal length such that s is a most frequent substring (i.e., occ_T(s) = M = max char frequency) and occ_T(s) is maximum. Also we are free to choose M? Actually M is determined by T; we can construct T accordingly.
We need s to occur at every occurrence of its first character c = s[0]. So we need to have exactly M occurrences of c, and at each of these positions, the substring s must appear. Also, no character appears more than M.
What is the minimal length T that can achieve a given M? And we can choose M arbitrarily? But we want overall minimal length, so we should minimize L subject to existence of some M. Also M must be at least 1 obviously.
But note: For a single s, we could set M=1. Then c appears exactly once in T. Then we need T to contain s starting at that position. We can simply set T = s. Then check: In T = s, what are frequencies? The maximum character frequency is max count of letters in s. Does s occur exactly once? Usually, occ_T(s) = 1 (since s appears exactly once, unless s appears overlapping? But if s contains itself as a proper substring overlapping? For example s = "aa", in T="aa", s appears twice? Actually occurrences of "aa" in "aa": starting at position 1 gives "aa"? Wait length=2, T length=2, only one occurrence starting at 1? Because substring of length 2 starting at 1 is the whole string. Overlapping occurrences require length? For "aa", could there be occurrence starting at 2? That would need index 2..3, but length 2, index 3 doesn't exist. So only one occurrence. So occ(s)=1. The maximum character frequency: count of 'a' is 2, so max char freq = 2. Then occ(s)=1 < 2, so s is not most frequent. So T=s with M=1 fails because we didn't ensure that c appears M times? Actually we required that c appears exactly M times, with M=1, c='a' appears once? In s="aa", 'a' appears twice, not once. So we cannot have M=1 because c appears twice, which would be > M. So M must be at least the count of c in T. But we control T. For s="aa", to have c appears exactly M and M is max char frequency, we need to place exactly M occurrences of 'a' as starts, but s also contains 'a' at second position. So total count of 'a' will be at least M + (number of extra 'a's from s that are not at start positions, considering overlaps). Overlaps could reduce total distinct positions of 'a'.
Let's try to construct T for s="aa". We need M occurrences of 'a' as starts. Let's denote start positions p1,...,pM. For each start pk, we require T[pk]='a', T[pk+1]='a'. So each start forces two positions to be 'a', possibly overlapping if consecutive starts are spaced by 1. For example, if we set p_k = 1,2,3,...,M? But then we need to define T over a range. Let's attempt to pack tightly: If we set starts at positions 1,2,...,M, then for start at 1: positions 1,2 = a,a. Start at 2: positions 2,3 = a,a, which forces position 3 = a. Continue, start at M: positions M, M+1 = a,a. So overall T would be a string of a's of length M+1. Then what is the count of 'a'? All positions are 'a', so count = M+1. But we also have M occurrences of 'a' as starts? Actually each position from 1 to M is a start (since each such position has character 'a'). But do we have exactly M starts? Yes, we designated starts at positions 1..M. However, note that the character 'a' appears at positions 1..M+1, so frequency of 'a' is M+1. The maximum character frequency is M+1. Is that equal to M? No, M is supposed to be the max frequency. But we set M as the number of starts? Wait, we defined M as the number of occurrences of the first character c (which is also the number of occurrences of s). In this construction, we placed M starts. But then the max char frequency became M+1 (since 'a' appears M+1 times). That would mean the maximum frequency is M+1, not M. Then s's frequency (M) would be less than max, so s is not most frequent. Therefore this construction fails. To satisfy condition, we need that the max character frequency is exactly M, which is the number of occurrences of s. That means that no character may appear more than M times. In this tightly packed construction, 'a' appears M+1 > M. So we need to reduce the count of 'a' to at most M.
But note: The character 'a' appears at start positions (M of them) and also at positions after each start (the second character). Overlaps reduce the number of distinct 'a' positions. In the above, we overlapped maximally: each start at i, the second position i+1 also serves as start for the next start if i+1 is a start. That made total distinct 'a' positions = M+1. Could we overlap more to reduce distinct 'a' count? Overlap can only happen when the second character of one occurrence coincides with the first character of another occurrence, i.e., the start positions differ by 1. That's already maximal overlap. So we get M+1 distinct positions. To have at most M distinct 'a' positions, we would need that the second character positions coincide with start positions in such a way that each 'a' appears in multiple roles. But each start contributes an 'a' at its position. The second character positions also must be 'a'. The only way to reduce total distinct 'a' count to M is if every second character position coincides with some start position, and also that no extra positions beyond the starts are needed. That would require that for each start pk, pk+1 equals some other start pj. That means the set of start positions is closed under +1 (i.e., if pk is a start then pk+1 is also a start, except possibly the last one). If we have M starts, and we want the set of positions {pk} ∪ {pk+1} to have size exactly M, that implies that the set of starts is exactly the set of positions from 1 to M, and the extra position M+1 is not included? But then the set of starts is {1,...,M}, and the union is {1,...,M+1}, size M+1. So not M. To achieve size M, we would need that the last position pk+1 for the maximum pk is also a start, meaning the starts extend beyond, increasing M. So essentially, if the string s has length Ls, then each occurrence of s contributes Ls positions. If we pack them overlapping maximally (i.e., each occurrence starts 1 position after the previous), the total length covered by M occurrences is Ls + M - 1. The number of distinct positions where 'a' appears depends on pattern. In s="aa", all positions are 'a'. So in the packed arrangement, all positions are 'a', count = Ls + M - 1 = M+1. So it's impossible to have total 'a' count ≤ M if M>=1? Let's check small M: M=1, then one occurrence of s: T must contain s as substring, and also the first character 'a' appears exactly once. If we take T = "a", but s="aa" cannot fit. So need length at least 2. If M=1, we need one start of 'a', and s of length 2 forces two positions both 'a', so T would be "aa". Then 'a' count=2 >1, so max freq=2, s freq=1, not most frequent. So M cannot be 1. M=2? Then we need 2 starts. Overlap: positions: if starts at 1 and 2, T length = 3, T = "aaa". Then 'a' count=3 >2. So still > M. M=3: starts at 1,2,3 => length 4, "aaaa", count=4 >3. Always count = M+1 > M. So it seems impossible for s="aa"? But is there a way to arrange starts not consecutively to reduce total 'a' count? If starts are spaced apart, then the second character positions may not coincide with starts, leading to extra 'a's. For example, starts at 1 and 3: then we need positions: 1:'a',2:'a' from first; 3:'a',4:'a' from second. So positions: 1,2,3,4 all 'a' -> count=4 for M=2, still >2. If starts at 1 and 4: positions:1,2='a','a'; 4,5='a','a' -> plus maybe position 3? Not forced to be 'a'. So positions: 1,2,4,5 are 'a'. That's 4 distinct positions, still >2. Actually, any start adds two positions unless the second position coincides with another start. To reduce total distinct 'a' positions, we want as much overlap as possible. Overlap only possible when distance between starts is less than length of s (2). So max overlap is distance 1. Then distinct positions = M+1. So minimal distinct 'a' positions = M+1. So 'a' count is at least M+1. Thus we need M >= 'a' count? But we need 'a' count ≤ M to keep max freq = M. So M+1 ≤ M impossible. Therefore no solution for s="aa". Indeed, the second example in problem statement: "kek", "preceq", "cheburek" -> answer NO. Possibly some strings inherently impossible.
Thus necessary condition: For each s, let c = s[0]. In any valid T, the total number of occurrences of each character must be ≤ M. In particular, consider character c itself. Its count in T is at least (number of occurrences as start) plus possibly additional occurrences from other positions within s (or from other s). Since we need exactly M occurrences of c as starts (because s must occur at each c), and the s itself may contain c in other positions. However, some of those other positions might coincide with start positions of other occurrences (maybe of same s or different s). So the minimal possible count of c given M occurrences of s (with starts) is something we need to compute.
We have a set of constraints from multiple strings. This looks like a system of equations akin to a graph of positions and characters.
Maybe we can think of it as designing a string T that is ultimately periodic with period equal to the greatest common divisor? Or perhaps the condition leads to that the strings s must all be "border"-like? Let's derive more generally.
Given T, define M = max char freq. For each s in S, we need:
-
freq_T(s) = M.
-
For its first character c, freq_T(c) = M (since occ(s) ≤ occ(c) and must equal M, so occ(c) must be at least M, but if occ(c) > M then max would be > M, so must be exactly M). So each first character of any s must appear exactly M times. So we have a set of characters C0 (those appearing as first character of some s) each with frequency exactly M.
-
Additionally, for each s, at every occurrence of c (i.e., at all M positions where c occurs), the substring starting there of length |s| must be exactly s. So the content of T at positions relative to c occurrences is fixed.
Now, what about characters that are not first characters of any s? They can appear at most M times, but may appear fewer.
Thus, for each character that appears in any s, we must ensure its total count ≤ M, but it might be forced to appear in many positions due to the substring constraints. This could cause contradictions if a character's required occurrences exceed M.
So the problem reduces to: Find integer M ≥ 1 and a string T such that:
-
There is a set of positions P_c for each character c in C0, with |P_c| = M.
-
For each s in S with first character c, for each position i ∈ P_c, we have T[i + j] = s[j] for j=0..|s|-1. (Thus, positions i+j are forced to characters.)
-
The forced assignments must be consistent (same position gets same character).
-
For each character x, the total number of positions forced to x (or later assigned) must be ≤ M. Actually, since M is the maximum frequency, we need for all characters x, count_T(x) ≤ M. And for c in C0, we need count_T(c) = M (since they appear at least at starts, and we can't have more than M). So we must have exactly M positions where character c appears, and those are exactly the start positions P_c? Not necessarily; could there be other positions where c appears that are not starts? That would increase count > M, which is not allowed. So for c in C0, all occurrences of c must be exactly the start positions (i.e., the positions where it appears as first character of its s). There cannot be any extra c elsewhere in T. So the forced positions for c from s constraints must include all occurrences of c, and they must be exactly M in total. This adds another condition: For each c in C0, the set of positions assigned character c must be exactly P_c (size M) and no other position can be c.
-
For other characters (not in C0), their total count must be ≤ M. They can appear in forced positions from s (e.g., other letters in s) and possibly other positions, but to minimize length we likely won't add extra positions, so we only have forced positions.
Thus, the problem becomes: We need to assign positions (indices) to characters, satisfying these constraints, with total length L minimized.
This is reminiscent of constructing a string from "samples" at multiple positions, with constraints on character counts. The positions are integer indices; we can choose the starting indices for each character set P_c, but they must be consistent across different characters because the forced assignments from different s may cross-interact.
We have multiple strings s. They might have different first characters. For each such s, it defines a pattern that must be repeated at each occurrence of its first character. That is, for each start position i where character c appears, the next |s|-1 characters are forced.
Thus, the whole string T is essentially determined by the positions of these first characters and the patterns. The constraints can be seen as equations on differences between positions.
Idea: Since each first character c appears exactly M times, we can think of T as being composed of M "threads" or "occurrences" of each s? Not exactly.
Alternate viewpoint: Consider the directed graph of positions. Suppose we fix an ordering of positions 1..L. For each s, its occurrences are at positions P_c. The values at those positions are forced to the pattern s. So we have a set of equations: For each s, for each i in P_c, for each offset j, T[i+j] = s[j].
If we think of the positions modulo some period? Perhaps the constraints force that the set of start positions for each character must be equally spaced, and the patterns must be consistent with a periodic string.
Let's try to derive necessary conditions from a single s.
For a single s, we need: There are exactly M occurrences of c (its first character). For each occurrence i, we have T[i..i+|s|-1] = s. The positions i are distinct. The total count of each character in T must be ≤ M, with c exactly M.
Now, consider the character s[1] (the second character if |s|>1). For each start i, T[i+1] = s[1]. So s[1] appears at least M times (if those positions are all distinct). However, they could coincide with start positions of some other character if s[1] = c? If s[1] = c, then some of those i+1 positions could be among the start positions of c (i.e., if i+1 is also a start position). That would reduce distinct count of s[1] because some positions are counted both as a start for c and as the second character of some occurrence. But the count of character s[1] overall must be ≤ M. So we need the number of distinct positions where s[1] is forced to appear (from these constraints) to be ≤ M. And if s[1] is not c, then these positions are forced to be s[1] and they must not exceed M; moreover, if s[1] appears elsewhere (maybe from other s), could increase count. But in single s case, the only forced positions for s[1] are the i+1 positions. Also, s[1] might itself be a first character of some other s, but in single s case there is no other.
Thus, for single s, we need that the M occurrences of the pattern can be arranged such that the total number of distinct positions assigned to each character is at most M, with c exactly M.
Moreover, we can choose the placement of the start positions arbitrarily (subject to consistency). The positions i can be any integers (representing indices in T). We want to minimize L, the total span from first to last forced position (maybe plus extra? but we can put characters before first start? If we want to minimize length, we likely start at position 1 with the earliest forced character, and end at last forced position). Also we could add extra characters before/after if needed, but that would increase length, so minimal T will have no extra characters beyond those forced by constraints? However, note that we need T to be a contiguous string. We can define T from 1 to L. The forced positions are some subset of [1, L]. We could also have gaps (positions with no forced character) which we can fill arbitrarily. But filling arbitrarily could increase counts of characters, potentially exceeding M. So we should fill them with characters that are not used much, or maybe we can leave them as some character that already has count M? But that would exceed M. So we should assign characters such that their counts don't exceed M. Since we aim to minimize length, we would avoid extra positions that are not forced, unless forced by some structure (like we need to have certain positions to satisfy overlapping constraints). Actually, the forced positions come from the pattern placements. The set of indices that get assigned by at least one occurrence of some s is the union over all s and all starts of the intervals [i, i+|s|-1]. We need to assign characters to all positions in [1, L] to form a string. Some positions may not be forced by any constraint; we can choose any characters for them, but we must ensure that the counts of characters do not exceed M. Since we want minimal L, we can try to choose L such that the forced positions exactly cover a contiguous segment without gaps, and we can set the unforced positions (if any) to characters that are already at maximum count? But that would risk exceeding M. Better to have no unforced positions, i.e., all positions from 1 to L are forced by at least one constraint. That would minimize L because any extra forced positions from pattern placements will define L. So minimal T likely consists exactly of the union of intervals forced by the occurrences, with L = max forced position - min forced position + 1, and we set T's indices accordingly. However, we might need to shift the whole set of positions to start at 1.
Thus, the problem reduces to: Choose integer M ≥ 1, and choose positions (integer indices) for the start of each occurrence of each first character (c in C0), with |P_c| = M, and for each s with first character c, enforce the pattern on intervals. The forced positions must be consistent, and the total distinct positions assigned to each character must be exactly M for c in C0 (and ≤ M for others). Also, the total span (max - min + 1) should be minimized.
Now, this looks like a problem about aligning multiple strings with periodicity. Another way: The condition that for a given s, the string s must appear at each occurrence of its first character implies that the positions of c must be such that if you look at T starting at those positions, you see s. This is similar to requiring that T is "synchronized" with respect to c. In particular, if we consider the string T, and we look at the set of positions where T matches s starting there, that set must be exactly the set of positions where T has character c, and there are exactly M of them. So the substring s occurs exactly at those positions, and c occurs exactly at those positions as well. Moreover, any other occurrence of c would cause extra occurrences of s? Actually if c occurs elsewhere, then since s must occur at every occurrence of c, that would create extra occurrences of s, increasing its count beyond M? Wait, s must have frequency exactly M. If c appears at some position that is not a start of an occurrence of s, then s would not occur there, violating the condition that s occurs at every occurrence of c. So c must not appear elsewhere. So indeed the occurrences of c are exactly the starts of s occurrences. So s occurs exactly M times.
Now, consider two different strings s and t with possibly same or different first characters. They impose constraints that may interrelate.
Let's attempt to derive constraints on the patterns themselves.
Suppose we have two strings s and t. Cases:
-
Same first character c. Then as argued, they must be identical because at each occurrence of c, both s and t must appear starting there, so s = t. So if there are two different strings with same first character, impossible -> output NO.
-
Different first characters: c and d. Then we have sets P_c and P_d each of size M. For each i in P_c, T[i..i+|s|-1] = s; for each j in P_d, T[j..j+|t|-1] = t.
Now, these intervals may overlap. Consistency constraints must hold. Also, characters c and d appear exactly at P_c and P_d respectively, nowhere else.
Moreover, consider the character at position i+1 for i in P_c: it's s[1]. If s[1] is some character e, then that position is forced to e. If e is a first character of some string (maybe c or d), then that position must belong to P_e (if e is in C0) because e must appear exactly at its start positions, and if a position is forced to be e, that position must be one of the starts of e (since e cannot appear elsewhere). So we get a relationship: For any character e that appears as a forced character at some position (due to some s), if e is in C0 (i.e., is first character of some string), then that position must be in P_e. Conversely, if e is not in C0, then it can appear at that position, but its total count must be ≤ M. But if it appears in many forced positions, it may exceed M, so we need to ensure its count ≤ M.
Thus, the forced positions for characters that are first characters must exactly match the sets of starts. This yields constraints linking the offsets between starts of different characters.
This resembles constructing a DFA or a set of equations based on string overlaps. Possibly the solution is to check if the set of strings can be arranged into a single "infinite" string that is periodic with period equal to the greatest common divisor of some lengths, but with finite truncation to M repeats.
Let's attempt to think of T as having a structure where the positions of first characters are spaced regularly with some pattern. Because if we have multiple characters, the constraints may force that the distance between occurrences of first characters is constant.
Consider a single s. Let’s denote positions of c: p1 < p2 < ... < pM. The constraints from s imply that for each k, T[p_k + d] = s[d] for d=0..|s|-1. In particular, for any offset d, the character at position p_k + d is forced to s[d]. If for some k1, k2, the positions p_k1 + d1 = p_k2 + d2, then we need s[d1] = s[d2]. This gives equations on the differences between p's.
If we want to minimize total length, we would pack the p's as closely as possible, likely with constant gap g between consecutive starts. Because if gaps vary, total length likely increases. But also need to satisfy character count constraints: for any character x (including those not in C0), the number of forced positions where T = x must be ≤ M. In particular, for c itself, we have exactly M positions (the starts). For other characters, if they appear too many times, we might exceed M. Overlaps can reduce distinct positions.
Thus, the problem seems to be about finding an arrangement (positions for starts) that minimizes total span while keeping each character's count ≤ M, with M as small as possible? But M is also variable: we can choose M. Larger M increases counts? Actually, larger M increases the number of starts, which may increase total length and also increase counts of characters. But the condition is that each character count ≤ M, so if we increase M, the allowed counts increase proportionally. However, the minimal length T likely corresponds to minimal possible M? Not necessarily; maybe we can achieve smaller total length with larger M due to better overlapping? Let's test with simple example.
Suppose we have a single s = "ab". Then c='a'. We need M occurrences of 'a' as starts. For each start, we set T[p_k]='a', T[p_k+1]='b'. Overlaps: if we place starts at positions 1,2,...,M, then T: positions: 1:a (start1), 2:b from start1 and also a from start2? Wait start2 at position 2 would require T[2]='a', but we already set T[2]='b' from start1, conflict because 'a' ≠ 'b'. So cannot overlap with distance 1 because that would cause conflict at overlapping positions: at position 2, we would need both 'b' (from start1) and 'a' (from start2). So overlapping with gap 1 is impossible unless s[0] = s[1]? Actually, for overlapping to be consistent, when two occurrences overlap, the overlapping parts must match. For starts at i and j (j>i), we need that for all d such that i+d = j + e, we have s[d] = s[e]. This is the condition for the string s to have a nontrivial overlap with itself. Specifically, if we have two occurrences of s starting at i and j, then the suffix of the first occurrence starting at offset (j-i) must equal the prefix of the second occurrence of length |s|-(j-i). That is, s must have a border of length |s|-(j-i) or equivalently, s[0..|s|-(j-i)-1] = s[j-i..|s|-1]. So overlapping is possible only if the shift (j-i) is a period of s (i.e., s is periodic with period (j-i)). In general, the set of possible distances between starts is limited by the self-overlap properties of s.
Thus, for a single s, the positions of starts (c) must be such that for any two starts with difference d, the string s must be consistent with shift d: s must be d-periodic at least for the overlapping part. More precisely, if we have occurrences at positions i and i+d, with d < |s|, then for the occurrences to not conflict, we need that for all t such that 0 ≤ t < |s| and t+d < |s|, s[t] = s[t+d]. That is, s must be d-periodic for the first |s|-d characters. If d ≥ |s|, then occurrences do not overlap, so no conflict.
Therefore, to minimize total length, we want to pack starts as closely as possible, i.e., use the smallest possible d that satisfies the periodicity condition. That smallest d is the minimal period p of s (the smallest positive integer such that s[i] = s[i+p] for all i from 0 to |s|-p-1). If s is not periodic at all (i.e., its minimal period = |s|), then the only way to avoid overlap conflicts is to ensure that the distance between any two starts is at least |s|. That would result in total length = (M-1)|s| + |s| = M|s| if we place them consecutively without overlap? Actually, if we place starts at positions 1, 1+|s|, 1+2|s|,..., then intervals don't overlap, total length = (M-1)|s| + |s| = M|s|. That yields distinct positions: each occurrence occupies |s| positions, non-overlapping, so total length = M*|s|. Count of characters: For c = s[0], appears M times (the starts). For other characters in s, they appear M times as well because they appear once per occurrence (non-overlapping). So each character's count = M * (frequency of that character in s). That could exceed M unless each character appears at most once in s. For s = "ab", characters: 'a' appears once, 'b' appears once. So with M occurrences, 'a' count = M, 'b' count = M. That's okay: both ≤ M, with 'a' exactly M (since it's first) and 'b' ≤ M (equal M, which is okay; it could be exactly M, then 'b' would also be a most frequent character? That's fine, as long as it doesn't exceed M). So for "ab", M could be any positive integer, and T could be "ab" repeated M times (concatenated). Then check: T = (ab)^M. Does s="ab" occur at every occurrence of 'a'? In T, positions of 'a' are at 1,3,5,... (odd positions). At each such position, the substring of length 2 is "ab"? At position 1: "ab", yes. Position 3: characters 3='a',4='b' -> "ab", yes. So s occurs at each 'a'. Also frequency of 'a' = M, frequency of 'b' = M, so max = M. s occurs M times, so it's most frequent. Also, any other substring? Possibly "ba" appears M-1 times, which is less than M. So condition satisfied. So T = (ab)^M works. Minimal length? For M=1, T="ab", length 2. Check: freq('a')=1, freq('b')=1, max=1, s occurs once, ok. So minimal length possible is 2. That is indeed minimal (non-empty string). So for "ab", answer would be "ab". But we must also consider lexicographically smallest among minimal length. For M=1, length 2 is minimal. Could there be a shorter string? length 1 impossible because s length 2. So answer is "ab". So the minimal T is simply s itself. But is there any other string with same length? "ab" is unique? Possibly "ba"? but s must be most frequent, so can't be. So answer "ab". That matches the idea: we can set M=1 and T=s, provided that the character frequencies in s satisfy: for each character x, count_s(x) ≤ 1? Wait for s="ab", count_s('a')=1, count_s('b')=1, both ≤1, and first character 'a' appears exactly 1 (M=1). That's okay. For s="aa", count_s('a')=2, which is >1 if M=1. So M=1 fails because 'a' count would be 2 > M. Could we set M=2? Then we need two occurrences of s, each length 2, but overlapping might reduce distinct 'a' count. We attempted overlapping but conflicts because s is not 1-periodic? Actually "aa" is 1-periodic (since s[0]=s[1]='a'), so minimal period p=1. So we can overlap with distance 1. Let's try M=2 with distance 1: starts at positions 1 and 2. Then T positions: start1: pos1='a', pos2='a'; start2: pos2='a', pos3='a'. So T = "aaa". But we need exactly M=2 occurrences of 'a' as starts? Actually start positions are positions 1 and 2. But in T, 'a' appears at positions 1,2,3 → count 3, not 2. So 'a' count = 3 > M=2. So fails. Could we arrange starts differently to reduce total 'a' count to exactly 2? Is it possible? We need exactly two positions where T[i]='a' and those are starts. But the pattern forces that at each start i, T[i+1]='a' as well. So the set of positions forced to 'a' includes the start positions and the positions start+1. To have only two distinct positions, we would need that for each start, the start+1 coincides with another start. With two starts, if we set starts at positions 1 and 2, then start+1 for start1 is 2, which is a start; start+1 for start2 is 3, which is not a start. So we get three positions. If we set starts at positions 1 and 1? Not allowed distinct. So with two starts, we must have at least 3 distinct positions because the largest start's +1 is beyond the last start unless the last start's +1 equals some start (impossible unless there is another start after). So with M starts, the minimal number of distinct positions forced to character c (which is also 'a') is M+1 when s[1]=c and overlapping with distance p=1. More generally, if s contains its first character at other positions, the count of that character may be higher.
Thus, for a single string s, the necessary and sufficient condition for existence of some M and T is that the string s has the property that for some positive integer M, we can arrange M occurrences of s (starting at positions where character c appears) such that the total count of each character does not exceed M, and c appears exactly M. Is there a known characterization? Let's analyze more systematically.
Define s, let c = s[0]. Let count of character x in s be f_x (frequency in s). When we place M occurrences of s starting at positions p1,...,pM, the total number of times character x appears in T (from these occurrences) is equal to the number of pairs (k, offset d) such that s[d] = x and position p_k + d is a distinct index. If occurrences overlap, some positions may be covered multiple times but they still contribute only one character (must be consistent). So the total count of x is the size of the set union over k of {p_k + d | s[d] = x}. However, if overlapping positions are forced to same character (by consistency), they are counted once. So we need to compute the minimum possible distinct positions that get character x given M occurrences placed with possible overlaps consistent with s.
Let’s think in terms of the string s and its shift compatibility. The set of start positions can be chosen as an arithmetic progression? Because to minimize total length and counts, it's natural to place them with constant shift equal to the minimal period p of s, as that allows maximal overlap while maintaining consistency. Indeed, if s has period p (i.e., s[i] = s[i+p] for all i where defined), then placing starts spaced by p will result in overlapping occurrences that are consistent. This yields a string that is periodic with period p, and the substring s repeats every p? Actually, if we set starts at positions 1, 1+p, 1+2p, ..., then the string T defined by these occurrences will be forced to be periodic with period p. Because for any position, it is covered by multiple occurrences, and the constraints enforce s[d] = s[d+p] for all d where both defined, which is exactly the period condition. So the minimal arrangement is to take the infinite periodic string with period p (the minimal period) and take a finite segment covering M occurrences such that there are exactly M starts within that segment. However, we need exactly M starts, and we need that the character c appears exactly M times (as starts). In a periodic string with period p, the character at positions that are starts must be c. But in the periodic extension, the character at positions that are congruent to some offset mod p might be c. If s has period p, then s[0] = c, and s[p] = s[0] if p < |s|, but s[p] may be defined only if p < |s|; otherwise if p = |s|, then there is no overlap.
We need to consider two cases: s is periodic with period p < |s|, or s is aperiodic (p = |s|).
Case 1: s is aperiodic (no nontrivial period). Then occurrences cannot overlap without conflict if distance < |s|. So to have M occurrences, we must place them with at least |s| distance between starts (or at least non-overlapping). The minimal total length is M * |s| (if we place them consecutively without gaps). In that arrangement, each character x appears exactly M * f_x times, where f_x is its frequency in s. For this to be ≤ M, we need M * f_x ≤ M for all x, i.e., f_x ≤ 1 for all x (except possibly c? but includes c). For c, f_c could be >1? If f_c > 1, then M * f_c > M unless f_c=1. So we need f_c = 1 and for all other x, f_x ≤ 1. In other words, s must have all distinct characters. Moreover, since c appears exactly once in s (at position 0), then count of c from M non-overlapping occurrences is M (since each occurrence contributes one c at start). That's exactly M, good. For other characters, each appears at most once per occurrence, so total count ≤ M if they appear at most once in s. So condition: s has all distinct characters. Then with M=1, we get T = s, length = |s|, and it works? Let's test: s="ab": distinct, M=1 works. s="abc": distinct, M=1 works: T="abc", freq('a')=1, 'b'=1, 'c'=1, max=1, s occurs once, most frequent. Yes. s="aba": characters: a appears twice, b once. Not all distinct. For M=1, counts: a appears 2 > M=1 => fails. For larger M, if we place non-overlapping, each occurrence contributes two a's, so total a count = 2M > M, fails. Could overlapping reduce a count? If s is aperiodic? But "aba" has period? "aba" length 3: check period 1? s[0]='a', s[1]='b' not equal, so no. period 2? s[0] vs s[2]: 'a'='a', s[1] vs s[3] out of range, so period 2 is possible? Actually period 2 means s[0]=s[2] (true) and length 3, so condition for period 2: s[0]=s[2] (only one check). Yes, "aba" has period 2 (since s[0]=s[2]). But also need s[1]? no further. So minimal period p=2? Typically period p is such that s[i]=s[i+p] for i=0..len-p-1. For len=3, p=2: check i=0: s[0]=a, s[2]=a ok. So p=2 < len, so s is periodic (with period 2). So overlapping with shift 2 is possible? But shift 2 is less than length 3, so overlapping would be of length 1. Let's examine: if we place starts at positions 1 and 3 (difference 2), then first occurrence covers 1-3, second covers 3-5. Overlap at position 3: first occurrence at position 3 is s[2]='a'; second occurrence at position 3 is s[0]='a'. They match 'a'='a', so consistent. So "aba" is periodic with p=2. So it's not aperiodic. So we need to handle periodic strings.
Case 2: s is periodic with minimal period p < |s|. Then we can place starts with spacing p, leading to overlapping occurrences. In the limit, we can pack them densely. The resulting string will be periodic with period p. In such a string, the count of each character per period can be computed. Let’s define the infinite periodic string P of length p that is the prefix of s of length p? Actually if s has period p, then s is a prefix of some infinite string with period p, and the infinite string is determined by s[0..p-1] (the base). Since s[0]=c, and s is periodic, we have s[i] = s[i mod p] for i < |s|. The base string t = s[0..p-1] (length p). Then s is t repeated floor(|s|/p) times plus a prefix? Actually since |s| may not be multiple of p, but period means s[i]=s[i+p] whenever both indices < |s|, so s is a prefix of the infinite repetition of t. So s = t[0..|s|-1] where t is repeated.
Now, if we place starts at positions that are congruent to some offset mod p, say positions 1, 1+p, 1+2p, ..., then the resulting T will be a segment of the infinite periodic string with period p. For a segment covering M starts, the total length L can be as low as (M-1)*p + |s|. Because the first start at position 1, last start at position 1+(M-1)p, and the last occurrence extends to (1+(M-1)p)+|s|-1 = (M-1)p + |s|. So L = (M-1)p + |s|. That's minimal possible if we pack with period p, because shifting by less than p would cause overlap conflict if p is minimal period; shifting by multiples of p is consistent. Could we shift by a multiple of p that is larger? That would increase length, so we take minimal p.
Now, what are the character counts in such a construction? The string T is a segment of length L of a periodic string with period p. The count of a character x in this segment can be computed based on its frequency in one period. Let count_period(x) = number of occurrences of x in t (period string). Then total count in T is roughly floor((L + shift) / p) * count_period(x) plus maybe partial at ends. But we need exact count.
But we also have the constraint that the first character c appears exactly M times (the starts). In this construction, the starts are at positions: 1, 1+p, 1+2p, ..., 1+(M-1)p. These are exactly the positions where the character equals c? Because at each start, we have T[i] = s[0] = c. Since T is periodic with period p, the character at positions congruent to 1 mod p is c (if the offset aligns). However, there may be other positions where character equals c, if c appears elsewhere in the period. That would increase the count of c beyond M, violating the requirement that c appears exactly M times (and no other c). Because c must appear only at start positions (since any occurrence of c would force an occurrence of s starting there, which would increase s's count beyond M if not a start? Actually, if there is another c not among the starts, then s would not necessarily occur there because we haven't forced it? But condition requires that s occurs at every occurrence of c. So if there is an extra c, then s must also start there, meaning that would be an additional start, increasing M. So we cannot have extra c's. Thus, in the constructed T, character c must appear exactly at the start positions and nowhere else.
In the periodic arrangement with period p, if the base period t contains c at positions other than the start offset, then those would be extra c's. So we need that in the period t, c appears only at one position per period, specifically at the offset corresponding to the start positions. Since the starts are spaced by p, the set of positions where c occurs in the infinite periodic string is the set of indices i such that t[(i-1) mod p] = c (assuming start offset 1). If t contains c at multiple positions, then c would appear more often. To have c appear exactly M times in the finite segment, we must ensure that in the segment, c occurs only at the start positions and not elsewhere. That means that within one period of length p, the character c must appear exactly once, and that occurrence must be at the position aligned with the starts. Moreover, the finite segment must be chosen such that no partial period at the ends introduces extra c's beyond the starts? But if the segment covers exactly from the first start to the end of the last occurrence, it may include extra c's at the end that are not starts. For example, s="aba", p=2, t="ab"? Actually s="aba", period p=2? Check: s[0]='a', s[2]='a', s[1]='b', no s[3]. So t = s[0..1] = "ab". Period is "ab". The infinite periodic string is "ababab...". Starts at positions where we put first character 'a'? We need starts at positions where character is 'a'? Since c='a'. In the periodic string, 'a' occurs at positions 1,3,5,... (odd). If we set starts at odd positions, then 'a' also occurs at positions 3? That's a start. But in the period "ab", 'a' appears only at position 0 (mod 2). So indeed only once per period. So that's good.
But consider