最长公共子串(1)--hdu1238(多个字符串得最长公共子串--暴力)

Substrings

                                                Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

Description

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


       这道题题意:给你n个字符串,然后要求求一个最大的子串(或者子串的反串,子串可以倒过来的意思)在每个字符串中都出现。要求求这个子串的长度。 

        由于最多十组样例,并且字符串个数最多100,每个字符串长度最多100,所以可以直接暴力枚举。为了节省时间,先找出这些串中长度最短的,然后枚举这个串中的所有子串。再让这个子串或者反串去匹配每一个串,若都符合,即输出,然后退出。这里枚举,当然是从子串长度大的往小的枚举啦,节省时间。 不过还是写了3重for循环,但是串的长度,还有串的数量都是100以内的。所以复杂度还可以接受。1000000. 代码:

#include
#include
char str[105][105];
char s1[105];
char s2[105];
int main()
{
	int T,n,i,j,k;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		int min=0;
		for(i=0;iMAX && flag)  
					MAX = l;
				flag = 1;
			}
		}
		printf("%d\n",MAX);
	}
	return 0;
}


你可能感兴趣的:(最长公共子串(1)--hdu1238(多个字符串得最长公共子串--暴力))