[算法学习笔记]寻找子字符串第一次出现位置
2014-05-27 14:07
232 查看
使用了需要回溯的BF算法和不需要回溯的KMP算法,本文纯属个人学习笔记。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
int BF(const char* src, const char* dst);
int KMP(const char* src, const char* dst);
void KMP_next(const char* src, int* next);
int BF(const char* src, const char* dst)
{
int i,j;
i = 0;
int srcLen = strlen(src);
int dstLen = strlen(dst);
while (i < srcLen)
{
j = 0;
while ((src[i] == dst[j]) && (j <dstLen))
{
i++;
j++;
}
if (j == dstLen)
{
return (i - j);
}
i = i - j + 1;
}
return -1;
}
int KMP(const char* src, const char* dst)
{
int next[100];
int i = 0, j = 0;
int srcLen = strlen(src);
int dstLen = strlen(dst);
KMP_next(src, next);
while (i < srcLen)
{
if ((-1 == j) || (src[i] == dst[j]))
{
i++;
j++;
}
else
{
j = next[j];
}
if (j == dstLen)
{
return (i - j);
}
}
return -1;
}
void KMP_next(const char* src, int* next)
{
int j = 0, k = -1;
next[0] = -1;
int srcLen = strlen(src);
while (j < (srcLen - 1))
{
if ((-1 == k) || (src[j] == src[k]))
{
j++;
k++;
next[j] = k;
}
else
{
k = next[k];
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
const char* src = "abcdegfhjfdscdcdhakjgrs";
const char* dst = "cdha";
//int position = BF(src, dst);
int position = KMP(src, dst);
return 0;
}
//
#include "stdafx.h"
#include <iostream>
using namespace std;
int BF(const char* src, const char* dst);
int KMP(const char* src, const char* dst);
void KMP_next(const char* src, int* next);
int BF(const char* src, const char* dst)
{
int i,j;
i = 0;
int srcLen = strlen(src);
int dstLen = strlen(dst);
while (i < srcLen)
{
j = 0;
while ((src[i] == dst[j]) && (j <dstLen))
{
i++;
j++;
}
if (j == dstLen)
{
return (i - j);
}
i = i - j + 1;
}
return -1;
}
int KMP(const char* src, const char* dst)
{
int next[100];
int i = 0, j = 0;
int srcLen = strlen(src);
int dstLen = strlen(dst);
KMP_next(src, next);
while (i < srcLen)
{
if ((-1 == j) || (src[i] == dst[j]))
{
i++;
j++;
}
else
{
j = next[j];
}
if (j == dstLen)
{
return (i - j);
}
}
return -1;
}
void KMP_next(const char* src, int* next)
{
int j = 0, k = -1;
next[0] = -1;
int srcLen = strlen(src);
while (j < (srcLen - 1))
{
if ((-1 == k) || (src[j] == src[k]))
{
j++;
k++;
next[j] = k;
}
else
{
k = next[k];
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
const char* src = "abcdegfhjfdscdcdhakjgrs";
const char* dst = "cdha";
//int position = BF(src, dst);
int position = KMP(src, dst);
return 0;
}
相关文章推荐
- 编写算法,求所有包含字符串S中而不包含字符串T中的字符串,构成的新串r以及r中每一个字符在S中第一次出现的位置
- 【学习笔记】初始化的位置判断,避免第一次执行后,无法跳出循环。
- 算法学习笔记之二:从1到n的正数中1出现的次数
- 算法笔记学习000——Smith-Waterman算法寻找两个字符串中匹配度最高的子串
- 函数strstr代码举例——从字符串haystack中寻找needle第一次出现的位置(不比较结束符NULL),没有就返回NULL。
- 算法学习笔记之寻找第1500个丑数
- [C++]Implement strStr() 找到子字符串第一次出现的位置
- 【php笔记-1】获取字符串里中文第一次出现的位置
- 在字符串中找到子字符串第一次出现的位置---笔试面试题
- 遗传算法与直接搜索工具箱学习笔记 五-----使用GPS算法寻找一个函数的最小值
- 算法---查找子串第一次出现的位置索引(indexOf)
- 对于一个有序数组,我们通常采用二分查找的方式来定位某一元素,请编写二分查找的算法,在数组中查找指定元素。 给定一个整数数组A及它的大小n,同时给定要查找的元素val,请返回它在数组中的位置(从0开始),若不存在该元素,返回-1。若该元素出现多次,请返回第一次出现的位置。
- 遗传算法与直接搜索工具箱学习笔记 五-----使用GPS算法寻找一个函数的最小值
- 易语言寻找文本第一次出现的位置
- 每天学习一点flash(35) 游戏设计笔记 (5) 跳跃算法笔记
- Silverlight学习笔记之使用TranslateTransform控制对象位置
- 算法学习笔记--排序(简介)
- 每天学习一点flash(35) 游戏设计笔记 (5) 跳跃算法笔记
- Effective C++ 学习笔记:避免public接口出现数据成员
- 学习笔记一:如何:对某词在字符串中出现的次数进行计数 (LINQ)