62 lines
1.3 KiB
Java
62 lines
1.3 KiB
Java
package chelper;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import io.InputReader;
|
|
import io.OutputWriter;
|
|
|
|
public class TaskI {
|
|
|
|
public static final int MAGIC_KEK = 1000000;
|
|
|
|
public void solve(int testNumber, InputReader in, OutputWriter out) {
|
|
int n = in.nextInt();
|
|
String[] strings = new String[n];
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
strings[i] = in.nextString();
|
|
}
|
|
|
|
String e = in.nextString();
|
|
|
|
int m = e.length();
|
|
|
|
int[] dp = new int[m + 1];
|
|
|
|
Arrays.fill(dp, MAGIC_KEK);
|
|
|
|
dp[0] = 0;
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
for (int j = 0; j < strings[i].length(); j++) {
|
|
int c = 0;
|
|
for (int k = 0; j + k < strings[i].length() && c < e.length(); k++) {
|
|
if (strings[i].charAt(j + k) == e.charAt(c)) {
|
|
c++;
|
|
dp[c] = Math.max(dp[c], 1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < m; i++) {
|
|
for (int j = 0; j < n; j++) {
|
|
int c = 0;
|
|
for (int k = 0; i + c < e.length() && k < strings[j].length(); k++) {
|
|
if (strings[j].charAt(k) == e.charAt(i + c)) {
|
|
c++;
|
|
dp[i + c] = Math.min(dp[i + c], dp[i] + 1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (dp[m] >= MAGIC_KEK) {
|
|
out.println(-1);
|
|
} else {
|
|
|
|
out.println(dp[m]);
|
|
}
|
|
}
|
|
}
|