【算法分析】字符串匹配:BF、KMP算法

字符串匹配算法:BF、KMP算法代码。

/*****************************************  
Copyright (c) 2015 Jingshuang Hu  
  
@filename:demo.c  
@datetime:2015.10.11  
@author:HJS  
@e-mail:eleftheria@163.com  
@blog:http://blog.csdn.net/hujingshuang  
*****************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
/**************************************************/
int text_read(char *S);
void BF_String_Matching(char *S, char *T);
void KMP_String_Matching(char *S, char *T);
int *Partial_Matching_Table(char *T);
/**************************************************/
int main()
{
	char *S = (char *)malloc(1000 * sizeof(char));
	char T[100];
	text_read(S);
	printf("给定文本串:\n%s", S);
	printf("\n输入模式串:\n");
	gets(T);
	printf("\n-------BF算法-------\n");
	BF_String_Matching(S, T);
	printf("\n-------KMP算法------\n");
	KMP_String_Matching(S, T);
	printf("\n\n");
	return 0;
}
/**************************************************/
int text_read(char *S)
{
	int len = 0;
	char ch;
	FILE *fp = fopen("test.txt","r");
	if (!fp)
	{
		printf("打开文件失败!\n");
		return 1;
	}
	while((ch = getc(fp)) != EOF)
	{
		S[len] = ch;
		len++;
	}
	fclose(fp);
	S[len] = '\0';
	S = (char *)realloc(S, (len + 1) * sizeof(char));
	return 0;
}
/**************************************************/
//暴力法
void BF_String_Matching(char *S, char *T)
{
	int S_length = strlen(S);
	int T_length = strlen(T);
	int i = 0, j = 0, same_length = 0;
	for (i = 0; i < S_length - T_length; i++)
	{
		for (j = 0; j < T_length; j++)
		{
			if (S[i + j] == T[j])
			{
				same_length++;
			}
			else
			{
				break;
			}
		}
		if (same_length == T_length)
		{
			printf("在位置%d发现匹配!\n", i + 1);
		}
		same_length = 0;
	}
}
/**************************************************/
//KMP   移动位数 = 已匹配的字符数 - 对应的部分匹配值
void KMP_String_Matching(char *S, char *T)
{
	int *table = Partial_Matching_Table(T);//获取部分匹配表
	int S_length = strlen(S);
	int T_length = strlen(T);
	int i = 0, j = 0;
	int same_length = 0, move_pos = 0;
	for (i = 0; i < S_length - T_length; i++)
	{
		if (S[i + 0] != T[0])//第一个字符都不等,则后移一位
		{
			continue;
		}
		else//否则,继续对比后面字符
		{
			same_length++;
			for (j = 1; j < T_length; j++)
			{
				if (S[i + j] == T[j])
				{
					same_length++;//已匹配数
				}
				else
				{
					move_pos = table[j - 1];
					break;
				}
			}
		}
		if (same_length == T_length)
		{
			printf("在位置%d发现匹配!\n", i + 1);
			i += T_length - 1;//直接往后移动T_length位
		}
		else
		{
			i += same_length - move_pos - 1;//移动后i的位置
		}
		same_length = 0;
	}
}
/**************************************************/
//部分匹配表
int *Partial_Matching_Table(char *T)
{
	int T_length = strlen(T);
	int *table = (int *)malloc(T_length * sizeof(int));
	int k = -1, i = 0, j = 0;
	memset(table, 0, T_length);//全部初始化成0
	table[0] = -1;
	while(j < T_length)
	{
		if ((-1 == k) || (T[k] == T[j]))
		{
			j++;
			k++;
			table[j] = k;
		}
		else
		{
			k = table[k];
		}
	}
	for (i = 0; i < T_length; i++)
	{
		table[i] = table[i + 1];
	}
	table = (int *)realloc(table, T_length * sizeof(int));
	return table;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值