Buckle 13. String search (common match and KMS)

Buckle 13. string search

https://www.lintcode.com/problem/implement-strstr/description

For a given source string and a target string, you should find the first position (starting at 0) in the source string where the target string appears. If it does not exist, it returns  -1.

 

Two methods: ordinary matching and KMS

 

Method 1: Common method

Idea: basic application capabilities, O (n2), string matching

As long as there is a mismatch in the middle, it will not work; it will not output until the last one is matched.

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
class Solution {
public:
	/**
	* @param source:
	* @param target:
	* @return: return the index
	*/
	int strStr(string &source, string &target)
	{
		// Write your code here
		if (source.size() == 0 && target.size() == 0)//鲁棒性
		{
			return 0;
		}
		if (target.size() == 0)//鲁棒性
		{
			return 0;
		}
		for (int i = 0; i < source.size(); i++)
		{
			if (source[i] == target[0])
			{
				for (int j = 0; j < target.size(); j++)
				{
					if (source[i + j] != target[j])break;//只要中间有一个不匹配都不行
					if (j == target.size() - 1)return i;//直到匹配到最后一个都相同时,才输出i
				}
			}
		}
		return -1;
	}
};

int main()
{
	Solution s;
	string source = "a";
	string target = "a";
	auto result = s.strStr(source, target);
	return 0;
}

 

Method 2: KMP

KMP

But the idea is correct

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;


class Solution {
public:
	/**
	* @param source:
	* @param target:
	* @return: return the index
	*/
	//
	int naive(string &source, string &target)
	{
		// Write your code here
		if (source.size() == 0 && target.size() == 0)//鲁棒性
		{
			return 0;
		}
		if (target.size() == 0)//鲁棒性
		{
			return 0;
		}
		int i = 0; int j = 0; int k = i;//从下标零开始计算,i记录主串位置,j记录
		while (i < source.size() && j < target.size())
		{
			if (source[i] == target[j])
			{
				i++;
				j++;
			}
			else
			{
				j = 0;
				k++;
				i = k;
			}
		}
		if (j >= target.size())
		{
			return k;
		}
		else
		{
			return -1;
		}
	}
	int KMP(string &source, string &target)
	{
		// Write your code here
		if (source.size() == 0 && target.size() == 0)//鲁棒性
		{
			return 0;
		}
		if (target.size() == 0)//鲁棒性
		{
			return 0;
		}
		int i = 0; int j = 0;//从下标零开始计算,i记录主串位置,j记录
		int next[20];
		//计算模式串的next数组
		getnext(next, target);
		while (i < source.size() && j <= target.size())
		{
			//j == 0,一种情况是模式串第一个字符target[0]开始识别,第二种情况是j按照next数组回退到next[0]时的情况
			//source[i] == target[j],如果相等继续比较下一个字符是否匹配
			if (j == 0 || source[i] == target[j])
			{
				i++;
				j++;
			}
			else
			{
				j = next[j];//i不回溯,j按照next数组回退
			}
		}
		if (j >= target.size())
		{
			int result123 = i - target.size();
			return (result123);
		}
		else
		{
			return -1;
		}
	}
	void getnext(int next[], string target)
	{
		int j = 0; int t = -1;//j代表字符的下标,t代表失配时下一步要移动的位置
		next[0] = -1;
		while (j < target.size())
		{
			if (t == -1 || target[j] == target[t])
			{
				next[j + 1] = t + 1;
				t++;
				j++;
			}
			else
			{
				t = next[t];
			}
		}
	}
};

int main()
{
	Solution s;
	string source = "abcdabcdefg";
	string target = "bcd";
	auto result = s.naive(source, target);
	auto resultKMP = s.KMP(source, target);
	return 0;
}

 

Published 23 original articles · praised 0 · visits 137

Guess you like

Origin blog.csdn.net/qq_35683407/article/details/105401337