1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class eks {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt(), m = sc.nextInt();
		Replica[] replications = new Replica[n];

		for (int i = 1; i <= n; i++) {
			int l = sc.nextInt();
			int[] replica = new int[l];
			for (int j = 0; j < l; j++) {
				replica[j] = sc.nextInt();
			}
			replications[i - 1] = new Replica(i, replica);
		}
		int[] sequence = new int[m];
		for (int i = 0; i < m; i++) {
			sequence[i] = sc.nextInt();
		}
		int[] replicaToFind = replications[0].result;

		// Evolves to given sequence already in first step (nah, if it was so easy..)
		if (contains(replicaToFind, sequence)) {
			System.out.println(2);
			return;
		}

		int depth = 3;
		ArrayList<int[]> sequencesToCheck = new ArrayList<int[]>(1);
		sequencesToCheck.add(sequence);
		while (true) {
			//System.out.println("Depth: " + depth);
			ArrayList<int[]> newSequencesToChecks = new ArrayList<int[]>();
			for (int[] sequenceToCheck : sequencesToCheck) {
				ArrayList<ArrayList<Replica>> total = new ArrayList<ArrayList<Replica>>();
				findMatching(sequenceToCheck, 0, replications, new ArrayList<Replica>(), total);

				if (!total.isEmpty()) {
					ArrayList<int[]> possibleSequences = generatePossibleSequences(total);

					for (int[] possibilities : possibleSequences) {
						if (contains(replicaToFind, possibilities)) {
							System.out.println(depth);
							return;
						}
						boolean contains = false;
						for (int[] alreadyAdded : newSequencesToChecks) {
							if (equals(alreadyAdded, possibilities)) {
								contains = true;
								break;
							}
						}
						if (!contains) {
							newSequencesToChecks.add(possibilities);
						}
					}
				}
			}
			if (newSequencesToChecks.isEmpty()) {
				System.out.println(-1);
				return;
			}
			depth += 1;
			sequencesToCheck.clear();
			sequencesToCheck = newSequencesToChecks;
		}
	}

	static void findMatching(int[] sequence, int idx, Replica[] replicas, ArrayList<Replica> currentResults, ArrayList<ArrayList<Replica>> total) {

		if (idx >= sequence.length) {
			//System.out.println("Found perfect matching: " + currentResults);
			total.add(currentResults);
			return;
		}

		int[] seq = subarray(sequence, idx);
		if (idx == 0) {
			for (Replica replica : replicas) {
				if (replica.result.length > seq.length) {
					if (contains(replica.result, seq)) {
						ArrayList<Replica> newResults = new ArrayList<Replica>(currentResults);
						newResults.add(replica);
						findMatching(sequence, idx + seq.length, replicas, newResults, total);
					}
				}
				for (int i = 0; i < replica.result.length; i++) {
					if (startsWith(seq, subarray(replica.result, i))) {
						ArrayList<Replica> newResults = new ArrayList<Replica>(currentResults);
						newResults.add(replica);
						findMatching(sequence, idx + replica.result.length - i, replicas, newResults, total);
					}
				}
			}
		} else {
			for (Replica replica : replicas) {
				// Whole replica must match
				if (replica.result.length <= seq.length) {
					if (startsWith(seq, replica.result)) {
						ArrayList<Replica> newResults = new ArrayList<Replica>(currentResults);
						newResults.add(replica);
						findMatching(sequence, idx + replica.result.length, replicas, newResults, total);
					}
					// Only left part of replica must match
				} else {
					if (startsWith(replica.result, seq)) {
						ArrayList<Replica> newResults = new ArrayList<Replica>(currentResults);
						newResults.add(replica);
						findMatching(sequence, idx + seq.length, replicas, newResults, total);
					}
				}
			}
		}
	}

	static ArrayList<int[]> generatePossibleSequences(ArrayList<ArrayList<Replica>> total) {
		ArrayList<int[]> sequences = new ArrayList<int[]>(total.size());
		for (ArrayList<Replica> replicas : total) {
			int idx = 0;
			int[] seq = new int[replicas.size()];
			for (Replica replica : replicas) {
				seq[idx++] = replica.source;
			}
			boolean contains = false;
			for (int[] currSequence : sequences) {
				if (equals(currSequence, seq)) {
					contains = true;
					break;
				}
			}
			if (!contains) {
				sequences.add(seq);
			}
		}
		return sequences;
	}

	static int[] subarray(int[] array, int from) {
		return Arrays.copyOfRange(array, from, array.length);
	}

	static boolean equals(int[] array, int[] seq) {
		if (array.length != seq.length) {
			return false;
		}
		for (int i = 0; i < array.length; i++) {
			if (array[i] != seq[i]) {
				return false;
			}
		}
		return true;
	}

	static boolean startsWith(int[] array, int[] prefix) {
		if (prefix.length > array.length) {
			return false;
		}
		for (int i = 0; i < prefix.length; i++) {
			if (array[i] != prefix[i]) {
				return false;
			}
		}
		return true;
	}

	static boolean contains(int[] array, int[] seq) {
		if (seq.length > array.length) {
			return false;
		}
		for (int i = 0; i < array.length - seq.length + 1; i++) {
			boolean contains = true;
			for (int j = 0; j < seq.length; j++) {
				if (array[j + i] != seq[j]) {
					contains = false;
					break;
				}
			}
			if (contains) {
				return true;
			}
		}
		return false;
	}

	static class Replica {
		int source;
		int[] result;

		public Replica(int source, int[] result) {
			this.source = source;
			this.result = result;
		}

		@Override
		public String toString() {
			return "Source: " + source + ", Result: " + Arrays.toString(result);
		}
	}
}