substring

//poj:2744
#include<stdio.h>
#include<string.h>

 int t,n;
 char str[101][101];
 
 int searchMaxSubString(char *source)
 {
 	int subStrLen=strlen(source);
 	int sourceStrLen=strlen(source);
 	int i,j;
 	bool foundMaxSubStr;
 	char subStr[101];
 	char revSubStr[101];
 	while(subStrLen>0)
 	{
 		for(i=0;i<=sourceStrLen-subStrLen;i++)
		 {
		 	strncpy(subStr,source+i,subStrLen);
		 	strncpy(revSubStr,source+i,subStrLen);
		 	subStr[subStrLen]=revSubStr[subStrLen]='\0';
		 	strrev(revSubStr);
		 	foundMaxSubStr=true;
		 	for(j=0;j<n;j++)
		 	{
		 		if(strstr(str[j],subStr)==NULL && strstr(str[j],revSubStr)==NULL)
		 		{
		 			foundMaxSubStr=false;
		 			break;
				}				
			}
			if(foundMaxSubStr) return subStrLen;
		 }
		 subStrLen--;	
	}
	return 0;
 }
 
 int main()
 {
 	int i,minStrLen,subStrLen;
 	char minStr[101];
 	scanf("%d",&t);
 	while(t--)
 	{
 		scanf("%d",&n);	
 		minStrLen=100;
 		for(i=0;i<n;i++)
 		{
 			scanf("%s",str[i]);
 			if(strlen(str[i])<minStrLen)
 			{
 				strcpy(minStr,str[i]);
 				minStrLen=strlen(minStr);
			 }
		}
		subStrLen=searchMaxSubString(minStr);
		printf("%d\n",subStrLen);
	}
 	return 0;
 }

posted on 2015-06-17 11:47  _noname  阅读(162)  评论(0编辑  收藏  举报