HDOJ1238(Substrings)
Substrings
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1268 Accepted Submission(s): 577
Problem 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
//1258362 2009-04-11 13:03:15 Accepted 1238 15MS 248K 1259 B C++ Xredman
#include <iostream>
#include <cstring>
using namespace std;
const int N = 105;
typedef char STR[N];
STR str[N];
int sti, n;
bool find(STR substr)
{
int i;
for(i = 0; i < n; i++)
if(i != sti && !strstr(str[i], substr))
return false;
return true;
}
int main()
{
int T, i, j, minlen, kk , cc;
bool mark;
STR substr;
cin>>T;
while(T-- && cin>>n)
{
sti = 0;minlen = -1;
for(i = 0; i < n; i++)
{
cin>>str[i];
if(minlen == -1 || strlen(str[i]) < minlen)
{
minlen = strlen(str[i]);;
sti = i;
}
}
kk = minlen;mark = false;
while(kk)
{
for(i = 0; i + kk <= minlen; i++)
{
for(cc = 0, j = i; cc < kk; j++, cc++)
substr[cc] = str[sti][j];
substr[cc] = '\0';
//cout<<"substr: "<<substr<<endl;
if(find(substr))
{
mark = true;
break;
}
}
if(mark)
break;
for(i = minlen - 1; i - kk >= -1; i--)
{
for(cc = 0, j = i; cc < kk; j--, cc++)
substr[cc] = str[sti][j];
substr[cc] = '\0';
//cout<<"substr2: "<<substr<<endl;
if(find(substr))
{
mark = true;
break;
}
}
if(mark)
break;
kk--;
}
cout<<kk<<endl;
}
}
#include <iostream>
#include <cstring>
using namespace std;
const int N = 105;
typedef char STR[N];
STR str[N];
int sti, n;
bool find(STR substr)
{
int i;
for(i = 0; i < n; i++)
if(i != sti && !strstr(str[i], substr))
return false;
return true;
}
int main()
{
int T, i, j, minlen, kk , cc;
bool mark;
STR substr;
cin>>T;
while(T-- && cin>>n)
{
sti = 0;minlen = -1;
for(i = 0; i < n; i++)
{
cin>>str[i];
if(minlen == -1 || strlen(str[i]) < minlen)
{
minlen = strlen(str[i]);;
sti = i;
}
}
kk = minlen;mark = false;
while(kk)
{
for(i = 0; i + kk <= minlen; i++)
{
for(cc = 0, j = i; cc < kk; j++, cc++)
substr[cc] = str[sti][j];
substr[cc] = '\0';
//cout<<"substr: "<<substr<<endl;
if(find(substr))
{
mark = true;
break;
}
}
if(mark)
break;
for(i = minlen - 1; i - kk >= -1; i--)
{
for(cc = 0, j = i; cc < kk; j--, cc++)
substr[cc] = str[sti][j];
substr[cc] = '\0';
//cout<<"substr2: "<<substr<<endl;
if(find(substr))
{
mark = true;
break;
}
}
if(mark)
break;
kk--;
}
cout<<kk<<endl;
}
}