关于串的kmp匹配问题
来源:互联网 发布:在淘宝不给退货怎么办 编辑:程序博客网 时间:2024/05/21 01:53
最近在看数据结构串的部分是,对串的匹配十分感兴趣,在严蔚敏老师的数据结构书上也讲到了对串的相关操作,而其中对串的匹配让人十分感兴趣。接下来就看看串的匹配算法。
1.朴素模式匹配算法 这里就不过多解释了
2.KMP模式算法匹配
具体的java代码如下
package nec.cn.stringComp;
public class KMPMatch
{
public static void main(String[] args)
{
String mainStr = "abcabcabdcabcabd";// 匹配串
String patternStr = "abcabd";// 模式串
int pLen = patternStr.length();
int next[] = new int[pLen];
caculate_next(next, patternStr);
for (int i = 0; i < pLen; i++)
{
System.out.println("计算得到next数组:");
System.out.print(next[i] + " ");
}
int find = match_KMP(mainStr, patternStr, next);
System.out.println("match position is " + find);
}
public static void caculate_next(int[] next, String p)
{
// 此处设定next数组的第0位和第1位的值为-1和0
next[0] = -1;
next[1] = 0;
int len = next.length;
for (int i = 2; i < len; i++)
{
if (p.charAt(i - 1) == p.charAt(next[i - 1]))
{
next[i] = next[i - 1] + 1;
} else
{
next[i] = 0;
}
}
}
public static int match_KMP(String m, String p, int[] next)
{
int mLen = m.length();
int pLen = p.length();
for (int i = 0, j = 0; i < mLen && j < pLen;)
{
if (m.charAt(i) == p.charAt(j))
{
// 如果i和j处的字符相等,则都加1
if (j == pLen - 1)
return i - j;
j++;
i++;
} else
{
// 不等的时候分两种情况讨论
if (-1 == next[j])
{
i++;
j = 0;
} else
j = next[j];
}
}
return -1;
}
}
对于其中的next[j]数组,详情可以查看严蔚敏老师的书。
- 关于串的kmp匹配问题
- KMP 解决串的模式匹配问题
- 浅谈KMP“串”的模式匹配问题
- KMP算法-串的匹配问题
- 数据结构(关于串的KMP匹配算法及其改进)
- 串匹配问题的BF算法和KMP算法
- 关于KMP模式匹配的理解
- 关于KMP算法(模块匹配)的实现
- 串的匹配:朴素匹配&KMP算法
- 串的KMP匹配算法
- 串的KMP匹配算法
- 串的kmp模式匹配
- codeforces 126B kmp串匹配问题
- 串的匹配模式 蛮力匹配 KMP匹配
- 关于线性字符串匹配的算法-----KMP的算法
- 简单的串的匹配kmp
- 关于KMP算法的理解——文本匹配算法
- 关于字符串匹配的效率 strstr 对比 KMP
- pppoe工作原理详解
- 栈
- Composer安装说明
- 线程之join()
- Qt之自定义搜索框
- 关于串的kmp匹配问题
- jquery 加载等待效果
- 面试题34:丑数
- OpenCV 轮廓的凸性
- 局域网内任何一台pc上windows下eclipse远程连接hbase数据库
- 最美好地祝福我们自己
- Python Built-in Funcitons
- Android用GridView画带背景色和点击事件的简单表格
- 在myeclipse中奖java项目打包成jar文件