暴力匹配算法(BF)与KMP算法分别实现字符串的模式匹配

设有两个字符串s和t,首先将s1与t1进行比较,直到s的某一个字符si和ti相同,再将它们之后的字符进行比较,若也相同,则如此继续往下比较,当s的某一个字符si与t的字符tj不同时,则s返回到本趟开始字符的下一个字符,即si-j+2,t返回到t1,继续开始下一趟的比较,重复上述过程。若t中的字符全部比较完,则说明本趟匹配成功,本趟的起始位置是i-j+1,否则,匹配失败。

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

int  BF_Index(std::string S, std::string T, int pos);//暴力匹配算法

int KMP_Index(std::string S, std::string T, int pos);//KMP算法

void getNext(std::string S, int  next[]);//获取next值

static std::string getCorrectString() {
   
	bool psw = false;                  //循环标志位
	std::string intput, str;
	do {
                                        //对于空格加以判断,防止读入错误
		psw = false;
		std::cin >> intput;
		str = str + intput;
		while (std::cin.peek() == ' ') {
    str = str + " "; std::cin.get()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值