HDU - 1238 Substrings

https://vjudge.net/problem/HDU-1238

HDU - 1238 Substrings

题目

You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring of any of the given strings.
Input
The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains a single integer n (1 <= n <= 100), the number of given strings, followed by n lines, each representing one string of minimum length 1 and maximum length 100. There is no extra white space before and after a string.
Output
There should be one line per test case containing the length of the largest string found.
Sample Input
2
3
ABCD
BCDFF
BRCD
2
rose
orchid
Sample Output
2
2

分析

直接拿第一个当模板串暴力循环即可

AC代码

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int n,t;
string s[110];
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n;
		int mi=210,u;
		for(int i=0;i<n;i++)
		{
			cin>>s[i];
			if(s[i].size()<mi)
			{
				mi=s[i].size();
				u=i;
			}
		}
		
		int ma=0;
		for(int i=0;i<mi;i++)
		{
			for(int j=0;j<=mi;j++)
			{
				string a,b;
				a=s[u].substr(i,j);
				b=a;
				reverse(a.begin(),a.end());
				int f=1;
				for(int i=0;i<n;i++)
				{
					if(s[i].find(a)==-1 && s[i].find(b)==-1)
					{
						f=0;
						break;
					}
				}
				if(f)
				{
					if(a.size()>ma)
						ma=a.size();	
				}
			}
		}
		cout<<ma<<endl;
	}
	return 0;
}
posted @ 2021-07-20 09:53  斯文~  阅读(26)  评论(0)    收藏  举报

你好!