设有两个字符串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);
void getNext(std::string S, int 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()