import java.util.*;
public class NaiveStringMatcher
{
public static char[] t = {'a', 'c', 'a', 'a', 'b', 'c'};
public static char[] p = {'a', 'a', 'b'};
public static void main(String[] args)
{
int n = t.length;
int m = p.length;
char[] tmp;
for (int s = 0; s <= n-m; s++)
{
tmp = Arrays.copyOfRange(t, s, n-m+s);
if (Arrays.equals(p, tmp))
{
System.out.println("Pattern occurs with shift" + s);
}
}
}
}
字符串匹配——朴素算法
最新推荐文章于 2025-05-25 23:12:27 发布