POJ 3080 Blue Jeans

求n个长度为60的字符串的最长连续公共子串


直接暴力枚举第一个串的每个子串,然后判断是否是后面串的子串即可

kmp的简单应用


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define rep(i, j, k) for(int i = j; i <= k; i++)

using namespace std;

int nex[100];

void pre (string p, int n)
{
	nex[0] = 0, nex[1] = 0;
	rep (i, 1, n - 1)
	{
		int j = nex[i];
		while (j && p[i] != p[j])
			j = nex[j];
		nex[i + 1] = p[i] == p[j] ? j + 1 : 0;
	}
}

bool work (string s, string p, int n)
{
	int m = s.length (), j = 0;
	rep (i, 0, m - 1)
	{
		while (j && s[i] != p[j])
			j = nex[j];
		if (s[i] == p[j])
			j++;
		if (j == n)
			return 1;
	}
	return 0;
}

bool better (string s, string p)
{
	return (s.length () > p.length () || (s.length () == p.length () && s < p));
}

string s[20], s0;
int tot, n;

int main ()
{
	int ti;
	cin >> ti;
	while (ti--)
	{
		string ans = "";
		cin >> tot;
		tot--;
		cin >> s0;
		rep (i, 1, tot)
			cin >> s[i];
		int len_s = s0.length ();
		rep (i, 0, len_s - 1)
			rep (k, i, len_s - 1)
		{
			string p = "";
			bool have = 1;
			rep (j, i, k)
				p += s0[j];
			n = p.length ();
			pre (p, n);
			rep (i, 1, tot)
				have &= work (s[i], p, n);
			//if (have) cout << p << endl;
			if (have && better (p, ans))
				ans = p;
		}
		if (ans.length () >= 3)
			cout << ans << endl;
		else
			cout << "no significant commonalities\n";
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值