题意是给出一些长度为60的字符串求这些字符串的最大连续共同子串。
刚开始看了别人的博客说时间给的很松暴力也能0ms过去,就直接写了个最笨的暴力以为能水过去,就是遍历第一个字符串每个字母开始的每个子串是否被后面的所有字符串包含找到最长的。竟然直接TLE了,剪了下枝把各个字母开始的确认为不行以后的子串的检查都给跳过就水过去了但是也花了141ms应该没那么水时间还是蛮紧的。
#include<iostream>
#include<Cstdio>
#include<string>
using namespace std;
string word[20];
int caseSize;
string ans;
int ansL;
bool find(string input)
{
int limit = 60 - input.length(), i, res = 0;
int j, l = input.length(); bool flag;
for(i = 1; i < caseSize; i++)
{
flag = false;
for(j = 0; j <= limit; j++)
{
if(word[i].substr(j, l) == input)
flag = true;
}
if(!flag)
return false;
}
return true;
}
void check(int start, int limit)
{
string temp; int i, j, k, p;
for(i = ansL; i <= limit; i++)
{
temp = word[0].substr(start, i);
if(find(temp))
{
p = temp.length();
if(p > ansL)
{
ansL = p;
ans = temp;
}
else if(temp.length() == ansL && temp < ans)
{
ans = temp;
}
}
else
return;
}
}
void cmp()
{
int i, limit;
for(i = 0; i < 60; i++)
{
limit = 60 - i;
check(i, limit);
}
}
int main()
{
int size, i;
cin >> size;
while(size--)
{
cin >> caseSize;
for(i = 0; i < caseSize; i++)
{
cin >> word[i];
}
ansL = 1; ans = "";
cmp();
if(ansL >= 3)
cout << ans << endl;
else
cout << "no significant commonalities" << endl;
}
return 0;
}