求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;
}