回朔法/KMP算法-查找字符串

时间:2023-03-09 02:54:35
回朔法/KMP算法-查找字符串
  1. 回朔法:在字符串查找的时候最容易想到的是暴力查找,也就是回朔法。其思路是将要寻找的串的每个字符取出,然后按顺序在源串中查找,如果找到则返回true,否则源串索引向后移动一位,再重复查找,直到找到返回true,或者源串查找完也没有找到返回false;这种方法简单粗暴,但思路清晰。又因为每次查找失败,源串需要回到起始位置的下一个位置开始查找,所以该算法也称为回朔法。
  2. KMP算法:先对要查找的字符串进行分析,找出其中重复的子字符串。然后将目标串与源串比较,一旦比较失败,则不用回朔,而是根据目标串子串的重复情况,直接调到重复子串的下一个位置。这样减少了大量的回朔,算法复杂度得意大量减小。

下面代码演示了回朔法和KMP算法,并作测试。

KMP算法查找:

 package org.lyk.impl;

 public class KMP<T>
{
public boolean comtains(T[] source, T[] target) throws Exception
{
if(source == null || target == null)
{
throw new Exception("比较目标不能为空");
} int sourceLength = source.length;
int targetLength = target.length;
if(targetLength > sourceLength)
//目标串长达大于源串,肯定不存在,直接返回false
return false;
else if(targetLength == sourceLength)
{
//两个串长度相等,则挨个比较,若发现任何一个元素不相等,则直接返回false,若所有元素均相等个,则返回true
return this.compair(source, target);
}
else
{//源串长度大于目标串长度,用KMP算法比较 //分析目标串的重复情况,生成next数组
int[] next = this.getNext(target);
int currentSourceIndex = ;
int currentTargetIndex = ; //如果目标串没有到末尾,则继续循环,如果已经到目标串末尾,则说明找到
while(currentTargetIndex < targetLength)
{
//如果源串下标越界,说明查找结束,乜没有找到结果,返回false
if(currentSourceIndex >= sourceLength)
return false; if(source[currentSourceIndex].equals(target[currentTargetIndex]))
{
//两个元素相等,目标串和源串下标向后各移动一位
currentSourceIndex++;
currentTargetIndex++;
}
else
{ //目标串的第一个元素就与源串不相等,则源串向后移动一个位置继续比较
if(currentTargetIndex == )
{
currentSourceIndex++;
} currentTargetIndex = next[currentTargetIndex];
}
}
return true;
}
} private int[] getNext(T[] target)
{
int[] next = new int[target.length];
if(next.length > )
{
for(int i = ; i<target.length; i++)
{
int count = ;
for(int j = ;j < i-; j++)
{
boolean flag = true;
for(int k = ; k <= j; k++)
{
if(target[k] != target[i--j+k])
{
flag = false;
break;
}
}
if(flag == true)
{
count = j+;
}
}
next[i] = count;
}
}
return next;
} private boolean compair(T[] source, T[]target)
{
for(int i = ; i < source.length; i++)
{
if(!source[i].equals(target))
return false;
}
return true;
}
}

回朔法(暴力比较)查找字符串:

 public static boolean strContain_BF(String _source, String _target)
{
boolean retVal = false;
if (null == _source || null == _target || "".equals(_source) || "".equals(_target))
return false;
char[] source = _source.toCharArray();
char[] target = _target.toCharArray();
int sourceLength = source.length;
int targetLength = target.length;
if (targetLength > sourceLength)
return false;
else if (targetLength == sourceLength)
{
for (int i = ; i < sourceLength; i++)
{
if (source[i] != target[i])
return false;
}
return true;
} else
{
for (int i = ; i <= sourceLength - targetLength; i++)
{
boolean flag = true;
for (int j = ; j < targetLength; j++)
{
if (target[j] != source[i + j])
{
flag = false;
break;
}
}
if (flag == true)
return true;
}
return false;
}
}