20240509J - Similarity (Easy Version)

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;
 } 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值