KMPアルゴリズムと応用

KMPアルゴリズム

基本的な

目標は:テキスト文字列内の文字列パターン表示され、位置の数、検索する
方法第一のパターンストリング・プリフィックス・テーブル計算、すなわち、次の[]配列は、文字列照合に次のアレイを使用:
ヒント
1.nextを私の最大の共通接頭辞と接尾辞のフロントサブ文字列の長さの長さ:[i]の意味

コードの実装

//经典KMP算法
//找出字符串p在字符串t中出现的次数与位置

#include <iostream>
#include <cstdio>
#include <cstring>

using  namespace std;

const int N = 1e6+10;
char  p[N],t[N]; //p为短的字符串,t为长的字符串
int next[N];	//数字next为前缀码表
int sum;		//字符串p在字符串t中出现的次数


void  getNext(const char P[], int next[]) {
	int  m = strlen(P);
	int i = 0, j;
	j = next[0] = -1;
	while (i < m) {
		while (-1 != j && P[i] != P[j])j = next[j];
		j++;i++;
		next[i] = j;
	}
}

void  kmp(const char P[], const char T[] , int next[]) {
	int n = strlen(T), m = strlen(P);
	int i, j;
	getNext(P, next);
	i = j = 0
		;
	while (i < n) {
		while (-1 != j && T[i] != P[j])j = next[j];
		i++; j++;
		if (j >= m) {
			sum++;
			printf("%d\n", i);
			j = next[j];//这儿修改很重要,不然会超时
		}
	}
}

int main() 
{
	strcpy(p,"aba");
	strcpy(t, "ababcabcbababcabacaba");
	kmp(p,t,next);
	printf("字符串匹配次数为:%d\n", sum);
	//system("pause");
	return 0;
}

アプリケーションKMPアルゴリズム

アプリケーションの次のアレイ

来源POJ2752は名声を求め、名前を求めます

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int N = 4 * 1e5 + 10;
void getNext(char P[], int next[])
{
	int m = strlen(P);
	int i, j;
	i = 0;
	j = next[0] = -1;
	while (i < m)
	{
		while (j != -1 && P[i] != P[j]) j = next[j];
		j++; i++;
		next[i] = j;
	}
}

int main()
{
	char S[N];
	int next[N];
	vector<int> a; //保存满足条件的姓名的长度
	while (scanf("%s", S)!= EOF)
	{
		a.clear();
		int len = strlen(S);
		a.push_back(len);
		memset(next,0, sizeof(next));
		getNext(S, next);
		while (next[len] > 0)
		{
			a.push_back(next[len]);
			len = next[len];
		}
		sort(a.begin(), a.end());
		for (int i = 0; i < a.size(); i++)
			cout << a[i] << " ";
		cout << endl;
	}
	return 0;
}

セクションの最小サイクルとサイクル

例一

出典POJ2406 パワー文字列の
アイデア:GET定理:と仮定文字列 S S 長さがあります リットル E n個 のみ 最小サイクルの日 L = リットル E n個 - n個 E バツ トン [ リットル E n個 ] L = EN-NEXT [のみ] 。もし L L 割り切れます リットル E n個 のみ そしてサイクル T = リットル E n個 / L LEN = T / L 。そうでない場合、文字列は循環ループ部の出力1から得ることができません。

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

using namespace std;

const int N = 1e6 + 10;

void getNext(char P[], int next[])
{
	int m = strlen(P);
	memset(next, 0, sizeof(next));
	int i, j;
	i = 0;
	j = next[0] = -1;
	while (i < m)
	{
		while (j != -1 && P[i] != P[j]) j = next[j];
		i++; j++;
		next[i] = j;
	}
}

int main()
{
	char S[N];
	int next[N];
	int L, T;//L为最小循环节的长度,T为循环周期
	while (scanf("%s", S) != EOF)
	{
		if (!strcmp(S, ".")) break;   //strcmp(str1,str2) :若 str1<str2 则返回负数;若str1>str2 则返回正数;若str1=str2 则返回0
		getNext(S, next);
		int len = strlen(S);
		int L = len - next[len];
		if (len%L == 0)
			cout << len / L << endl;
		else
			cout << 1 << endl;
	}	
	return 0;
}

例二

出典POJ1961 Peroidsの
アイデア:この質問の拡張セクションが得られるケーススタディ、および所定の組成物のi番目の文字列の前にサブのサイクルごと。ループ部の長さを横断するときに文字列全体を、決定することができる横切ります L = - n個 E バツ トン L = I-次の(I)

//最小循环节与循环周期问题
//最小循环节: L = len - next(len)
//循环周期: T = len/L (L可以整除len)

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

using namespace std;

const int N = 1e3 + 10;

void getNext(char P[], int next[])
{
	int m = strlen(P);
	memset(next, 0, sizeof(next));
	int i, j;
	i = 0;
	j = next[0] = -1;
	while (i < m)
	{
		while (j != -1 && P[i] != P[j])
			j = next[j];
		j++; i++;
		next[i] = j;
	}
}

int main()
{
	int next[N];
	char S[N];
	char s1[N];
	int L, T;		//L为最小循环节,T为循环周期
	int k;
	int q = 0;
	while (scanf("%d", &k) != EOF)
	{
		if (k == 0) break;
		q++;
		scanf("%s", S);
		cout << "Test case #" << q << endl;
		getNext(S, next);
		for (int i = 2; i <= k; i++)
		{
			L = i - next[i];
			if (i%L == 0&& L<i)
			{
				T = i / L;
				cout << i << " " << T << endl;
			}
		}
	}
	system("pause");
	return 0;
}
リリース9件のオリジナルの記事 ウォンの賞賛4 ビュー1021

おすすめ

転載: blog.csdn.net/L18273121172/article/details/89713583