找N个字符串的最长连续子串,这里我是用暴力枚举水过的。。好土
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
namespace
{
bool cmp(const string &s1, const string &s2)
{
return s1.size() < s2.size();
}
void put(const string &s, set<string> &S)
{
for (size_t i = 0; i < s.size(); i++)
for (size_t len = 1; len + i <= s.size(); len++)
S.insert(s.substr(i, len));
}
}
int main()
{
int t;
cin >> t;
vector<string> V;
set<string> S;
while (t--)
{
int n;
cin >> n;
V.clear();
string s;
for (int i = 0; i < n; i++)
{
cin >> s;
V.push_back(s);
}
sort(V.begin(), V.end(), cmp);
S.clear();
put(V[0], S);
reverse(V[0].begin(), V[0].end());
put(V[0], S);
size_t max = 0;
for (set<string>::iterator it = S.begin(); it != S.end(); it++)
{
bool find = true;
for (size_t i = 1; i < V.size(); i++)
{
if (V[i].find(*it) == string::npos)
{
reverse(V[i].begin(), V[i].end());
if (V[i].find(*it) == string::npos)
{
find = false;
break;
}
}
}
if (find && (*it).size() > max)
max = (*it).size();
}
cout << max << endl;
}
return 0;
}