您的位置:首页 > 其它

HDU--1238-Substrings(-最长连续子串)

2017-07-12 10:15 330 查看
Description
You are given anumber of case-sensitive strings of alphabetic characters, find the largeststring X, such that either X, or its inverse can be found as a substring of anyof the given
strings. 
 
Input
The first lineof the input file contains a single integer t (1 <= t <= 10), the numberof test cases, followed by the input data for each test case. The first line ofeach test case
contains a single integer n (1 <= n <= 100), the number ofgiven strings, followed by n lines, each representing one string of minimumlength 1 and maximum length 100. There is no extra white space before and aftera string. 
 
Output
There should beone 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
 

 

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
int main()
{
int t;
char a[1005][1005],str[1005],restr[1005];
int l[1005];
cin>>t;
while(t--)
{
int n,i,j;
cin>>n;
//	printf("n=%d\n",n);
for(i=0;i<n;i++)
{
scanf("%s",a[i]);
l[i]=strlen(a[i]);
//		printf("a[%d]=%s\n",i,a[i]);
}
int max=0,min=99999,x,y;
for(j=0;j<i;j++)
{
if(max<l[j])
{
max=l[j];
x=j;
}
if(min>l[j])
{
min=l[j];
y=j;
}
}
//
4000
printf("max=%d,min=%d,a[%d]=%s,a[%d]=%s\n",max,min,min,a[y],max,a[x]);
int q=min;
int p=min;
int o=1;
int d;
while(q>0)
{

for(i=0;i<=p-q;i++)
{
d=1;
strncpy(str,a[y]+i,q);
strncpy(restr,a[y]+i,q);
str[q]=restr[q]='\0';
strrev(restr);
//	printf("str=%s,restr=%s\n",str,restr);
for(j=0;j<n;j++)
{
if(strstr(a[j],str)==NULL&&strstr(a[j],restr)==NULL)
d=0;
}
if(d==1)
{
printf("%d\n",strlen(str));
o=0;
break;
}

}
q--;
if(o==0)
break;

}
if(q==0&&d==0)
printf("0\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: