バックル13.文字列検索(一般的な一致とKMS)

バックル13.文字列検索

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

特定のソース文字列とターゲット文字列について、ソース文字列の中でターゲット文字列が出現する最初の位置(0から開始)を見つける必要があります。存在しない場合はを返します  -1

 

2つの方法:通常のマッチングとKMS

 

方法1:一般的な方法

アイデア:基本的なアプリケーション機能、O(n2)、文字列マッチング

途中で不一致がある限り、機能せず、最後のものが一致するまで出力されません。

#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;
}

 

方法2:KMP

KMP

しかし、アイデアは正しいです

#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;
}

 

元の記事を23件公開 賞賛された0 訪問数137

おすすめ

転載: blog.csdn.net/qq_35683407/article/details/105401337