匹配算法的错误答案 [Java]
Wrong Answers for Matching Algorithm [Java]
我遇到过这种在整数数组中查找模式的匹配算法。它需要一个数组 s,它具有要在其中进行匹配的序列。另一个数组具有模式 p,它具有要匹配的模式。
// Example: Match {1, 2} in {1, 3, 1, 2, 3} =>
// after finding the first 3, skips ahead to the second 1, then finds {1, 2} at 2.
public static int match2(final int[] s, final int[] p) {
for (int i = 0; i <= s.length - p.length; i++) {
int j;
for (j = 0; j < p.length; j++) {
if (t[i + j] != p[j]) {
i += j; // Mismatch, skip ahead.
break;
}
}
if (j == p.length) {
return i;
}
}
return -1;
}
是否有任何输入可以使这段代码给出错误的输出?
在 {1,1,2} 中找不到 {1,2}。
我遇到过这种在整数数组中查找模式的匹配算法。它需要一个数组 s,它具有要在其中进行匹配的序列。另一个数组具有模式 p,它具有要匹配的模式。
// Example: Match {1, 2} in {1, 3, 1, 2, 3} =>
// after finding the first 3, skips ahead to the second 1, then finds {1, 2} at 2.
public static int match2(final int[] s, final int[] p) {
for (int i = 0; i <= s.length - p.length; i++) {
int j;
for (j = 0; j < p.length; j++) {
if (t[i + j] != p[j]) {
i += j; // Mismatch, skip ahead.
break;
}
}
if (j == p.length) {
return i;
}
}
return -1;
}
是否有任何输入可以使这段代码给出错误的输出?
在 {1,1,2} 中找不到 {1,2}。