The easy version and the hard version are different problems, yet it is crucial to retain your solution code once it’s accepted, as it might be useful later on.
Many place names bear similarities. Take, for instance, Suzhou
and Quzhou
. They are considered similar because they share a common substringuzhou. A substring is a contiguous sequence of characters within a string. For example, in the string abcd, bc is a substring of it, but acis not.We define the similarity between two strings as the length of their longest common substring. Thus, the similarity between Suzhou and Quzhouis 5, and between Hangzhou and Chengdu it is 2。Given n
place names, your task is to find two names that have the maximum similarity and output the similarity.
Input
The first line contains one integer T (1≤T≤15), indicating the number of test cases.For each test case, the first line contains an integer n
(2≤n≤50), indicating the number of places. Each of the following n
lines contains a string s (1≤|s|≤50), indicating the place names. The place names are guaranteed to consist only of lowercase English letters.
It is guaranteed that the sum of n over all test cases does not exceed 300
.
Output
For each test case, output an integer in a single line representing the maximum similarity.
Examples
Input
2
2
jiangsu
xiangtan
3
hangzhou
chengdu
wuxi
Output
4
2
#include<bits/stdc++.h>
using namespace std;
int solve()
{
int n,maxx=0,ff=0;
// string t;
cin>>n;
string s[55];
for(int i=1;i<=n;i++)
cin>>s[i];
for(int i=1;i<n;i++)
{ //两个循环截取
for(int j=i+1;j<=n;j++)
{
for(int k=0;k<s[j].size();k++)
{ //再用两个循环截取
for(int z=1;z<=s[j].size();z++)
{
string t=s[j].substr(k,z);//判断截取的是否能找到
if(s[i].find(t)!=-1)
{
int tt=t.size();
maxx=max(maxx,tt);
}
}
}
}
}
cout<<maxx<<endl;
return 0;
}
int main()
{
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}