Simpliciy + KMP + BM 字符串搜索算法

Simplicity

#include <stdio.h>
#include <string.h>

int simplicity(char *s, char *t, int pos);

int simplicity(char *s, char *t, int pos)
{
	int slen = strlen(s);
	int tlen = strlen(t);

	int i = pos;
	int j = 0;

	while(i < slen && j < tlen) {
		if(s[i] == t[j]) {
			i++;
			j++;
		} else {
			i = i - j + 1;
			j = 0;
		}
	}

	// subscripts start at 0 : j == len
	// subscripts start at 1 : j > len
	if(j == tlen) {
		return i - j;
	}

	return -1;
}

int main()
{
	int pos = 0;
	char *s = "goodgoogle";
	char *t = "goog";

	printf("{s:‘%s‘, t:‘%s‘, pos:‘%d‘, result(subsripts start at 0):‘%d‘}\n", s, t, pos, simplicity(s, t, pos));
	return 0;
}

KMP

#include <stdio.h>
#include <string.h>

void get_nextSubscripts(char *t, int *next);
int kmp(char * s, char *t, int pos);

void get_nextSubscripts(char *t, int *next)
{
	int tlen = strlen(t);

	int i = 0; // suffix
	int j = -1; // prefix

	next[0] = -1;

	while(i <= tlen) {
		if(j == -1 || t[i] == t[j]) {
			i++;
			j++;

			if(t[i] == t[j]) {
				next[i] = next[j];
			} else {
				next[i] = j;
			}
		} else {
			j = next[j];
		}
	}
}

int kmp(char *s, char *t, int pos)
{
	int next[255];
	int slen = strlen(s);
	int tlen = strlen(t);

	int i = pos;
	int j = -1;

	get_nextSubscripts(t, next);

	while(i < slen && j < tlen) {
		if(j == -1 || s[i] == t[j]) {
			i++;
			j++;
		} else {
			j = next[j];
		}
	}

	if(j == tlen) {
		return i - j;
	}

	return -1;
}

int main()
{
	int pos = 0;
	char *s = "goodgoogle";
	char *t = "goog";

	printf("{s:‘%s‘, t:‘%s‘, pos:‘%d‘, result(subsripts start at 0):‘%d‘}\n", s, t, pos, kmp(s, t, pos));

	return 0;
}

BM

时间: 2024-08-30 01:10:21

Simpliciy + KMP + BM 字符串搜索算法的相关文章

grep之字符串搜索算法Boyer-Moore由浅入深(比KMP快3-5倍)

这篇长文历时近两天终于完成了,前两天帮网站翻译一篇文章“为什么GNU grep如此之快?”,里面提及到grep速度快的一个重要原因是使用了Boyer-Moore算法作为字符串搜索算法,兴趣之下就想了解这个算法,发现这个算法一开始还挺难理解的,也许是我理解能力不是很好吧,花了小半天才看懂,看懂了过后就想分享下,因为觉得这个算法真的挺不错的,以前一直以为字符串搜索算法中KMP算很不错的了,没想到还有更好的,Boyer-Moore算法平均要比KMP快3-5倍. 下面是我对该算法的理解,参考了一些关于该

字符串搜索算法

http://dsqiu.iteye.com/blog/1700312 BF(Brute Force)算法 1.思想 2.编程实现 暴力算法,又称朴素算法,是最基本的字符串搜索算法,当然也是效率最低的算法. 3.时间复杂度 时间复杂度为O(m*n) //m与n分别为2个字符串的长度 4.补充资料 KMP(Knuth-Morris-Pratt)算法 1.思想 2.编程实现 暴力算法,又称朴素算法,是最基本的字符串搜索算法,当然也是效率最低的算法. 3.时间空间复杂度 4.补充资料 http://w

字符串搜索算法Boyer-Moore的Java实现

由于是毕业后转行的原因,所以本人在工作之前没有系统的学过数据结构.算法导论之类的课.说白了就是没有这样的底蕴,哈哈.所以这篇博客主要是写给自己看的,因为时间有限,本人写的内容估计远远不会有大家期待的那么详细,所以,可以此文可以选择性的忽略哦. 算法介绍:关于Boyer-Moore算法(后面简称BM算法)的概念网上一搜一大把.所以这里就不做具体阐述了.有疑问的建议参考阮一峰的这篇文章(此文文笔细腻且又通俗易懂): 阮一峰:字符串匹配的Boyer-Moore算法 算法精髓:这个字符串查找算法高效的原

poj 3461 字符串单串匹配--KMP或者字符串HASH

http://poj.org/problem?id=3461 先来一发KMP算法: #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <iostream> #include <cmath> #include <map> #include <queue> using namespace std;

UVA - 10298 Power Strings (KMP求字符串循环节)

Description Problem D: Power Strings Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiati

BoyerMoore字符串搜索算法

BoyerMoore 字符串搜索算法,返回pat在txt中第一次出现的起始位置,若不存在则返回-1,算法复杂度为O(N), 最坏为O(M*N) (M.N分别为pat与txt的长度). 1 #include <vector> 2 #include <list> 3 #include <map> 4 #include <set> 5 #include <queue> 6 #include <deque> 7 #include <st

POJ--2406Power Strings+KMP求字符串最小周期

题目链接:点击进入 事实上就是KMP算法next数组的简单应用.假设我们设这个字符串的最小周期为x 长度为len,那么由next数组的意义,我们知道len-next[len]的值就会等于x.这就是这个题目的关键点. 代码例如以下: #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=1000000+100; char str[maxn]; in

codeforces 825F F. String Compression dp+kmp找字符串的最小循环节

/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: dp[i]表示前i个字符需要的最小次数. dp[i] = min(dp[j]+w(j+1,i)); (0<=j<i); [j+1,i]如果存在循环节(自身不算),那么取最小的循环节x.w = digit((i-j)/x)+x; 否则w = i-j+1; 求一个区间最小循环节: 证明:http://w

2017.8.7 联考水题 Passward kmp/hash 字符串

你来到了一个庙前,庙牌上有一个仅包含小写字母的字符串 s. 传说打开庙门的密码是这个字符串的一个子串 t,并且 t 既是 s 的前缀又是 s 的后缀并且还在 s 的中间位置出现过一次. 如果存在这样的串,请你输出这个串,如有多个满足条件的串,输出最长的那一个. 如果不存在这样的串,输出"Just a legend"(去掉引号). 输入格式: 仅一行,字符串 s. 输出格式: 如题所述 样例输入 fixprefixsuffix 样例输出: fix 数据范围: 对于 60%的数据, s 的