匹配算法[Java]的错误答案

问题描述 投票:0回答:1

我遇到了这种匹配算法,该算法可以找到整数数组中的模式。它需要一个数组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;
}

是否有任何输入可以使这段代码给出错误的输出?

java matching
1个回答
1
投票

无法在{1,1,2}中找到{1,2}。

© www.soinside.com 2019 - 2024. All rights reserved.