#K34527. Min Operations to Make the String Beautiful

    ID: 25329 Type: Default 1000ms 256MiB

Min Operations to Make the String Beautiful

Min Operations to Make the String Beautiful

You are given a string \( s \) of length \( n \) and \( m \) special substrings. A string is considered beautiful if every one of the special substrings appears in \( s \) as a contiguous subsequence. Your task is to determine the minimum number of operations needed to make \( s \) beautiful. In one operation, you may perform modifications on \( s \). However, in this problem, you are not allowed to modify \( s \). Therefore, if every special substring is already present in \( s \), the answer is 0; otherwise, it is impossible and you should output -1.

Note: Although the problem statement mentions operations, the only valid answers are 0 (if \( s \) is already beautiful) or -1 (if it is impossible to achieve beauty with the allowed operations).

Formally, given a string \( s \) and a list of special substrings \( \{t_1, t_2, \dots, t_m\} \), output 0 if for every \( i \) (\(1 \le i \le m\)), \( t_i \) is a substring of \( s \), and -1 otherwise.

inputFormat

The input is given as follows:

  • The first line contains two integers \( n \) and \( m \) separated by a space, where \( n \) is the length of the string \( s \) and \( m \) is the number of special substrings.
  • The second line contains the string \( s \) consisting of \( n \) lowercase English letters.
  • The following \( m \) lines each contain a non-empty string representing a special substring.

If \( m = 0 \), then there are no special substrings.

outputFormat

Output a single integer:

  • Output 0 if every special substring is found in \( s \).
  • Otherwise, output -1 if at least one special substring does not appear in \( s \).
## sample
10 2
abcdefghij
abc
fgh
0

</p>