查找字符串是我们平常编程过程中经常遇到的,现在介绍一种查找字符串算法,增加程序的执行速度。
通常我们是这么写的:
/*
content: search a string in a othor string
author: lw
date: 2015-01-30
target: kmp algorithm
*/ #include <stdio.h>
#include <string.h> void compare(char * sourcestr, char * targetstr)
{
char *moveSource, *moveTarget, *headPoint;
int num = ;
headPoint = sourcestr;
moveSource = sourcestr;
moveTarget = targetstr;
while(*headPoint != *moveTarget)
headPoint++;
moveSource = headPoint;
while(*moveSource != '\0' && *moveTarget != '\0')
{
num++;
moveSource++;
moveTarget++;
if(num == )
break;
while(*moveSource != *moveTarget)//-->kmp
{
headPoint = headPoint + ;
moveSource = headPoint;
num = ;
moveTarget = targetstr;
}
}
printf("%s\n", headPoint);
} int main()
{
char *source = "mabveacabiabcy";
char *target = "abc";
char *sourcestr;
char *targetstr;
sourcestr = (char *)malloc( * sizeof(char));
targetstr = (char *)malloc( * sizeof(char));
memset(sourcestr, '\0', * sizeof(char));
memset(targetstr, '\0', * sizeof(char));
strncpy(sourcestr, source, );
strncpy(targetstr, target, );
compare(sourcestr, targetstr);
return ;
}
现在把函数 compare() 函数中的内 while() 中的内容改进一下:
说明:拿字符串 mabveacabiabcy 来说,当查找到字符 v 时发现和 abc 中的 c 不同,则指向字符串 mabveacabiabcy 中的第二个字符的指针就要移动,如果不使用 kmp 算法,那么指针移动一位,如果使用 kmp 算法,则指针移动两位,因为当比较到字符 v 时我们实际已经知道 v 以前的字符是什么了,所以可以断定不止要移动一位,具体移动几位就和字符串 abc 有关了,要判断其是否回文字符串,此例中 abc 对应数组 1,2,0 。
修改后的代码如下:
/*
content: search a string in a othor string
author: lw
date: 2015-01-30
target: kmp algorithm
*/ #include <stdio.h>
#include <string.h> void compare(char * sourcestr, char * targetstr)
{
char *moveSource, *moveTarget, *headPoint;
int num = ;
headPoint = sourcestr;
moveSource = sourcestr;
moveTarget = targetstr;
while(*headPoint != *moveTarget)
headPoint++;
moveSource = headPoint;
while(*moveSource != '\0' && *moveTarget != '\0')
{
num++;
moveSource++;
moveTarget++;
if(num == )
break;
while(*moveSource != *moveTarget)//-->kmp
{
if(num > )
headPoint = headPoint + num;
else
headPoint = headPoint + ;
moveSource = headPoint;
num = ;
moveTarget = targetstr;
}
}
printf("%s\n", headPoint);
} int main()
{
char *source = "mabveacabiabcy";
char *target = "abc";
char *sourcestr;
char *targetstr;
sourcestr = (char *)malloc( * sizeof(char));
targetstr = (char *)malloc( * sizeof(char));
memset(sourcestr, '\0', * sizeof(char));
memset(targetstr, '\0', * sizeof(char));
strncpy(sourcestr, source, );
strncpy(targetstr, target, );
compare(sourcestr, targetstr);
return ;
}