【POJ1226】Substrings
来源:Substrings
代码:
#include <iostream>
#include <string.h>
using namespace std;
#define MAX 101
int t,n;
char str[100][MAX];
char minStr[MAX];
void Init()
{
int minLen=MAX;
cin >> n;
for( int i=0;i<n;i++ )
{
cin >> str[i];
if( strlen(str[i]) < minLen )
{
minLen = strlen(str[i]);
strcpy(minStr,str[i]);
}
}
}
//貌似poj不支持strrev,只好自己定义
void strrev(char *revStr,char *str)
{
int len = strlen(str);
for( int i=len-1;i>=0;i-- )
{
revStr[len-i-1]=str[i];
}
revStr[len] = '\0';
}
int MaxSubstring()
{
int sourceLen = strlen(minStr);
int subStrLen = strlen(minStr);
char subStr[MAX],revSubStr[MAX];
bool found;
while( subStrLen )
{
for( int i=0;i<=sourceLen-subStrLen;i++ )
{
found=true;
strncpy(subStr,minStr+i,subStrLen);
//strncpy(revSubStr,minStr+i,subStrLen);
subStr[subStrLen] = '\0';
//revSubStr[subStrLen] = '\0';
strrev(revSubStr,subStr);
for( int j=0;j<n;j++ )
{
if( strstr(str[j],subStr) == NULL && strstr(str[j],revSubStr)
== NULL )
{
found = false;
break;
}
}
if( found )
return subStrLen;
}
subStrLen--;
}
return subStrLen;
}
int main()
{
int len;
cin >> t;
while(t--)
{
Init();
len = MaxSubstring();
cout << len << endl;
}
return 0;
}