最长公共子串匹配代码实现

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<vector>
#include<string>
#include<cstring>
using namespace std;

string GetLCS(string s1, string s2) {
	vector< vector<int> > v(s1.length(), vector<int>(s2.length()));
	int maxLen = 0, maxEnd = 0;
	for (int i = 0; i < static_cast<int>(s1.length()); i++)
		for (int j = 0; j < static_cast<int>(s2.length()); j++) {
			if (s1[i] == s2[j]) {
				if (i == 0 || j == 0) {
					v[i][j] = 1;
				}
				else {
					v[i][j] = 1 + v[i - 1][j - 1];
				}
			}
			else {
				v[i][j] = 0;
			}

			if (v[i][j] > maxLen) {
				maxLen = v[i][j];
				maxEnd = i;
			}
		}
	return s1.substr(maxEnd - maxLen + 1, maxLen);
}

char * LargesrCommonStr(char * str1, char * str2) {
	char *longstr, *shortstr, *substr;

	if (str1 == NULL || str2 == NULL) {
		cout << "不存在与空串的最大相同子序列" << endl;
		return NULL;
	}
	//确认两个字符串中的较短串
	if (strlen(str1) < strlen(str2)) {
		shortstr = str1;
		longstr = str2;
	}
	else {
		shortstr = str2;
		longstr = str1;
	}

	//直接判断较短串是否是较长串的子串
	if (strstr(longstr, shortstr) != NULL)
		return shortstr;

	//寻找最大的共同子串
	substr = (char *)malloc(sizeof(char) * strlen(shortstr));
	//如果不是,则从较短串长度减一起来依次构造不同长度的子串
	//如果某个长度的子串匹配,则可以直接返回
	for (int i = strlen(shortstr) - 1; i >= 1; i--) {
		//构造同一长度的不同子串序列
		//只要能确定不同子序列的不同起始位置即可
		for (int j = 0; j <= static_cast<int>(strlen(shortstr) - i); j++) {
			strncpy(substr, &shortstr[j], i);
			substr[i] = '\0';
			if (strstr(longstr, substr) != NULL) {
				return substr;
			}
		}
	}

	//如果没有任何可以匹配的子串,释放点不必要的空间
	if (substr != NULL) {
		free(substr);
		substr = NULL;
	}
	return NULL;
}

//找出所有的最长公共子串
void  GetAllLcs(char *str1, char *str2, vector<string> &Lcs) {
	char * longstr, *shortstr, *substr;

	if (str1 == NULL || str2 == NULL) {
		cout << "存在空串,无最大公共子串" << endl;
		return;
	}

	if (strlen(str1) < strlen(str2)) {
		longstr = str2;
		shortstr = str1;
	}
	else {
		longstr = str1;
		shortstr = str2;
	}

	if (strstr(longstr, shortstr) != NULL) {
		Lcs.push_back(shortstr);
	}

	substr = (char*)malloc(sizeof(char) * strlen(shortstr));

	int i, j;

	for (i = static_cast<int>(strlen(shortstr) - 1); i >= 1; i--) {
		substr = (char *)malloc(sizeof(char) * i);
		for (j = 0; j <= static_cast<int>(strlen(shortstr) - i); j++) {
			strncpy(substr, &shortstr[j], i);
			substr[i] = '\0';
			if (strstr(longstr, substr) != NULL) {
				Lcs.push_back(substr);
			}
		}
	}
}

void OutputAllLcs(char * str1, char *str2) {
	vector<string> Lcs;
	GetAllLcs(str1, str2, Lcs);
	if (Lcs.size() == 0) {
		cout << "没有公共子串" << endl;
	}
	else {
		cout << "公共子串有:" << endl;
		/*for (int i = 0; i < static_cast<int>(Lcs.size()); i++) {
			printf("%s\n", Lcs[i]);
		}*/
		vector<string>::iterator itr;
		int len = Lcs[0].length();
		for (itr = Lcs.begin(); itr != Lcs.end(); ) {
			cout << "长度为" << len << "的公共子串:" << endl;
			while( itr!=Lcs.end()  && len == (*itr).length()) {
				cout << (*itr) << " ";
				if (itr == (Lcs.end() - 1)) {
					return;
				}
				itr++;
			}
			cout << endl;
			len = (*itr).length();
		}

	}
}

//找出某一特定长度的所有公共子串
void GetLcsLenSpecified(char *str1, char *str2, int len, vector<string> &LcsSpecified) {
	char *longstr, *shortstr, *substr;

	if (str1 == NULL || str2 == NULL) {
		cout << "存在空串" << endl;
		return;
	}

	if (strlen(str1) < strlen(str2)) {
		longstr = str2;
		shortstr = str1;
	}
	else {
		longstr = str1;
		shortstr = str2;
	}

	if (strstr(longstr, shortstr) && len == strlen(shortstr)) {
		LcsSpecified.push_back(shortstr);
		return;
	}

	substr = (char*)malloc(sizeof(char) * strlen(shortstr));

	int  j;

	for (j = 0; j <= strlen(shortstr) - len; j++) {
		strncpy(substr, &shortstr[j], len);
		substr[len] = '\0';
		if (strstr(longstr, substr)) {
			LcsSpecified.push_back(substr);
		}
	}

}

void OutputLcsLenSpecified(char *str1, char *str2, int len) {
	vector<string> LcsSpecified;
	GetLcsLenSpecified(str1, str2, len, LcsSpecified);
	if (LcsSpecified.size() == 0) {
		cout << "没有长度为" << len << "的公共子串" << endl;
	}
	else {
		cout << "长度为" << len << "的公共子串有:" << endl;
		vector<string>::iterator itr;
		for (itr = LcsSpecified.begin(); itr != LcsSpecified.end(); itr++) {
			cout << (*itr)<<" ";
		}
	}
}

int main() {
	//string s1, s2;
	//cin >> s1;
	//cin >> s2;
	//string lcs = GetLCS(s1, s2);
	//cout << lcs << endl;

	char str1[100];
	char str2[100];
	cout << "请输入str1" << endl;
	cin >> str1;
	cout << "请输入str2" << endl;
	cin >> str2;

	cout << "最大公共子串为:" << LargesrCommonStr(str1, str2) << endl;

	OutputAllLcs(str1, str2);

	cout << endl;
	OutputLcsLenSpecified(str1, str2, 3);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值