poj 3080 hdu 1238 暴力KMP解决

poj 3080 hdu 1238 暴力KMP解决

poj 3080,<—原题链接

hdu 1238,<—原题链接

题意

poj 3080 是说给你n个字符串,找出他们共有的最长字符串,但是如果找到的字符串长度小于3,也算失败,并输出相应的语句,否者查找成功,输出找到的字符串。

hdu 1238和上边的差不多,不同的是,找到的这个字符串R有可能不是这个n个字符串中某个字符串T的子串,但是如果R的逆串R'T的子串的话,也算成功。

解题思路

这个题好像没看到什么更有效的方法,暴力枚举,数据量也比较小。

对于这两个题,我们的做法都是直接按照第一个字符串枚举所有的子串,然后看看剩下的字符串是不是都含有这个字符串,就这么简单,就这么直接,勇敢枚举吧少年。

代码实现

这两个代码真的是极其相似

//poj 3080
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<stack>
#include<queue>
#include<map>
using namespace std;
const int MAXN=12;
const int MAX_LEN=67;
string str[MAXN];
int m, nx[MAX_LEN];
void get(string &s)
{
	int j=0, k=-1, len=s.length();
	nx[0] = -1;
	while(j<len)
	{
		if(k == -1 || s[j] == s[k])
		{
			j++; k++;
			nx[j] = k; 
		}
		else k = nx[k];
	}
}
bool KMP(string &s1, string &s2)
{
	int i=0, j=0, len1=s1.length(), len2=s2.length();
	while(i<len1)
	{
		if(j == -1 || s1[i] == s2[j])
		{
			i++; j++;
		}
		else j = nx[j];
		if(j == len2)
			return true;
	} 
	return false;
}
int main()
{
	int t;
	scanf("%d", &t);
	while(t--)
	{
		string ans=""; 
		scanf("%d",&m);
		for(int i=0; i<m; i++)
			cin >> str[i];
		for(int k=1; k<=str[0].length(); k++)
		{
			for(int i=0; i + k <= str[0].length(); i++) //开始的位置 
			{
				string mod = str[0].substr(i, k);
				get(mod);
				int count = 1; //记录所有匹配的个数 
				for(int j=1; j<m; j++)
				{
					if(KMP(str[j], mod) == true)
						count++; 
				}
				if(count == m)
				{
					if(mod.length() > ans.length())
						ans = mod;
					else if( mod.length() == ans.length() )
						ans = min(ans, mod);
				}
			}
		}
		if(ans.length() < 3)
			printf("no significant commonalities
");
		else cout<<ans<<endl;	
	} 
	return 0;
}
//hdu 1238
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<stack>
#include<queue>
#include<map>
using namespace std;
const int MAX_LEN=107;
string str[MAX_LEN];
int m, nx[MAX_LEN];
void get(string &s)
{
	int j=0, k=-1, len=s.length();
	nx[0] = -1;
	while(j<len)
	{
		if(k == -1 || s[j] == s[k])
		{
			j++; k++;
			nx[j] = k; 
		}
		else k = nx[k];
	}
}
bool KMP(string &s1, string &s2)
{
	int i=0, j=0, len1=s1.length(), len2=s2.length();
	while(i<len1)
	{
		if(j == -1 || s1[i] == s2[j])
		{
			i++; j++;
		}
		else j = nx[j];
		if(j == len2)
			return true;
	} 
	return false;
}
int main()
{
	int t;
	scanf("%d", &t);
	while(t--)
	{
		int ans=0;
		scanf("%d",&m);
		for(int i=0; i<m; i++)
			cin >> str[i];
		for(int k=1; k<=str[0].length(); k++)
		{
			for(int i=0; i + k <= str[0].length(); i++) //开始的位置 
			{
				string mod = str[0].substr(i, k);
				get(mod);
				int count = 1; //记录所有匹配的个数 
				for(int j=1; j<m; j++)
				{
					if(KMP(str[j], mod) == true)
						count++; 
				}
				if(count == m)
				{
					if(mod.length() > ans)
						ans = mod.length();
				}
				count=1;
				for(int j=1; j<m; j++)
				{
					reverse( str[j].begin(), str[j].end() );
					if(KMP(str[j], mod) == true)
						count++;
				}
				if(count == m)
				{
					if(mod.length() > ans)
						ans = mod.length();
				}
			}
		}
		printf("%d
", ans);	
	} 
	return 0;
}
欢迎评论交流!
原文地址:https://www.cnblogs.com/alking1001/p/12247748.html