算法导论-第32章-字符串匹配:Rabin-Karp算法C++实现

#include 
#include 
#include 
using namespace std;

int char_to_int(char c) {
	return c - '0';
}

int module(int x, int q) {
	if (x < 0) {
		return x % q + q;
	}
	else {
		return x % q;
	}
}

bool is_forge(char* P1, char* P2, int m) {
	for (int i = 0; i < m; ++i) {
		if (P1[i] != P2[i]) {
			return true;
		}
	}
	return false;
}

void rabin_karp(char* T, char* P, int d, int q, int m, int n) {
	int x = pow(d, m - 1);
	int h = module(x, q);
	int p{};
	int* t = new int[n - m] {};
	for (int i = 0; i < m; ++i) {
		p = module(d * p + char_to_int(P[i]), q);
		t[0] = module(d * t[0] + char_to_int(T[i]), q);
	}
	for (int s = 0; s < n - m - 1; ++s) {
		if (p == t[s]) {
			if (!is_forge(P, T + s, m)) {
				cout << "Pattern occurs with shift " << s - 1 << endl;
			}
		}
		else {
			t[s + 1] = module(d * (t[s] - char_to_int(T[s]) * h) + char_to_int(T[s + m]), q);
		}
	}
	delete[]t;
}


int main(int argc, char* argv[]) {
	vector v{};
	int d{ 10 };
	cout << "please enter the radix : " << endl;
	cin >> d;
	int q{};
	cout << "please enter the mod number (prime better) : " << endl;
	cin >> q;
	char element{};
	cout << "please enter the pattern (end with 'a') :" << endl;
	while (cin >> element) {
		if (element != 'a') {
			v.push_back(element);
		}
		else {
			break;
		}
	}
	int m = v.size();
	char* P = new char[m] {};
	int index{};
	for_each(v.begin(), v.end(), [=](char x)mutable{ P[index++] = x; });
	v.clear();
	cout << "please enter the text (end with 'a') :" << endl;
	while (cin >> element) {
		if (element != 'a') {
			v.push_back(element);
		}
		else {
			break;
		}
	}
	int n = v.size();
	char* T = new char[n] {};
	index = 0;
	for_each(v.begin(), v.end(), [=](char x)mutable{ T[index++] = x; });
	rabin_karp(T, P, d, q, m, n);
	delete[]T;
	delete[]P;
	return 0;
}

你可能感兴趣的:(算法)