65,189
社区成员




#include <stdio.h>
#include <string.h>
const unsigned int kBufferSize = 105;
char g_shortest[kBufferSize];
char g_lines[100][210];
void reverse(char dest[], char source[])
{
unsigned int length = strlen(source);
for (int i = 0; i < length; ++i)
{
dest[i] = source[length - 1 - i];
}
dest[length] = '\0';
}
// lineNo starts from zero.
void scanLine(int lineNo)
{
char line[kBufferSize];
scanf("%s", line);
if ((strlen(g_shortest) == 0) || (strlen(line) < strlen(g_shortest)))
{
strcpy(g_shortest, line);
}
strcpy(g_lines[lineNo], line);
// Concatenate the reverse.
reverse(g_lines[lineNo] + strlen(line) - 1, line);
}
int getCommonLength(int lineNum)
{
int len = strlen(g_shortest);
while (len > 0)
{
// Enum all substrings of length "len" in "shortest",
// and test if that's contained by other strings.
//for (int i = 0; i < strlen(g_shortest); i += len
for (int i = 0; len <= strlen(g_shortest) - i; ++i)
{
char tmp = g_shortest[i + len];
// Mark the new(temp) end.
g_shortest[i + len] = '\0';
// Search all strings to see if this is a substring.
int j;
for (j = 0; j < lineNum; ++j)
{
if (!strstr(g_lines[j], g_shortest + i))
{
break;
}
}
// Restore the old.
g_shortest[i + len] = tmp;
if (j == lineNum)
{
return len;
}
}//for
--len;
}//while
// No common string found.
return 0;
}
int testCase()
{
g_shortest[0] = '\0';
int lineNum = 0;
scanf("%d", &lineNum);
for (int i = 0; i < lineNum; ++i)
{
scanLine(i);
}
return getCommonLength(lineNum);
}
int main()
{
int tests;
scanf("%d", &tests);
for (int i = 0; i < tests; ++i)
{
printf("%d\n", testCase());
}
return 0;
}