#P6681. String Concatenation Ambiguity
String Concatenation Ambiguity
String Concatenation Ambiguity
Given N strings, each with a length of at most M, you are allowed to concatenate these strings in any order (each string may be used any number of times as long as the resulting string’s length does not exceed a certain limit). Your task is to determine the minimum length L for which there exist two different concatenation methods that produce the exact same string. Two methods are considered different if the order (or selection) of the strings used is different, even if the resulting string is the same.
If such an ambiguous string exists, output its minimal length. Otherwise, output -1
.
Note: In the concatenation process, the same string can appear multiple times and the concatenation order matters. The problem guarantees that all formulas appear in \( \LaTeX \) format where applicable (e.g. \(N\), \(M\)).
inputFormat
The first line contains two integers \(N\) and \(M\), where \(N\) is the number of strings and \(M\) is the maximum length of each string.
Then follow \(N\) lines, each line containing one non-empty string (whose length is at most \(M\)).
outputFormat
Output a single integer representing the minimum length of an ambiguous string that can be formed by concatenating the given strings in different orders. If no such string exists within the search limit, output -1
.
sample
2 2
a
aa
2