//字符串子串朴素查找
int searchSubStr(char aSub[],char b[])
{
int temp = 0;
size_t len = 0;
if (strlen(b)-strlen(aSub) > 0)
{
len = strlen(b)-strlen(aSub);
}
else if (strlen(b)-strlen(aSub) == 0)
{
len = strlen(b);
}
for (int i = 0;i < len; ++i)
{
int j = 0;
while ((j < strlen(aSub))&&(b[i + j] == aSub[j]))
{
j++;
}
if (j == strlen(aSub))
{
return 0;
}
if ((j > 0)&&(j > temp))
{
temp = i;
}
}
if (temp)
{
return temp;
}
return -1;
}
//字符先处理
void searchSame(char a[][20],int n)
{
char tempA[1000];
char tempB[1000];
char tempResult[1000];
memset(tempA, 0,sizeof(tempA));
memset(tempB, 0,sizeof(tempB));
memset(tempResult, 0,sizeof(tempResult));
for (int i = 0;i < n; ++i)
{
memset(tempA, 0,sizeof(tempA));
if (i == 0)
{
strcpy(tempA, a[i]);
}
else
{
strcpy(tempA, tempResult);
}
int subLen = (int)strlen(a[i]);
for (int j = i+1;j < n;++j)
{
int len = (int)strlen(a[j]);
memset(tempB, 0,sizeof(tempB));
strcpy(tempB, a[j]);
if (subLen > len)
{//字符交换
char tempC[1000];
memset(tempC, 0,sizeof(tempC));
strcpy(tempC, tempA);
memset(tempA, 0,sizeof(tempA));
strcpy(tempA, tempB);
memset(tempB, 0,sizeof(tempB));
strcpy(tempB, tempC);
}
if (strcmp(tempA, tempB) == 0)
{
continue;
}
int findex = searchSubStr(tempA,tempB);
if (findex > 0)
{
strcat(tempResult, tempB+findex+1);
break;
}
else if (findex == 0)
{
if (strlen(tempResult) < strlen(tempB))
{
strcpy(tempResult, tempB);
}
break;
}
}
}
printf("%s",tempResult);
}