Code: Select all

```
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
int N,index;
char bank[55][100],temp[100];
int compare (int a, int b)
{
int i,j,len1,len2,len;
len1=strlen(bank[a]);
len2=strlen(bank[b]);
if(len1>len2)
len=len2;
else
len=len1;
for(i=0;i<len;i++)
{
if(bank[a][i]>bank[b][i]) // return 0 if a is greater
return 0;
else if(bank[a][i]<bank[b][i]) // return 1 if b is greater
return 1;
}
if(abs(len1-len2)==1) // X and XXXXy consideration
{
if(len1>len2)
{
if(bank[a][len1-1]<=bank[b][0])
return 1;
else
return 0;
}
else if(len2>len1)
{
if(bank[b][len2-1]<=bank[a][0])
return 0;
else
return 1;
}
}
else // X and XXXyz.. consideration
{
if(len2>len1)
return 0;
else
return 1;
}
}
void swap(int i, int j)
{
char dump[100];
strcpy(dump,bank[j]);
strcpy(bank[j],bank[i]);
strcpy(bank[i],dump);
}
void sort(int k)
{
int i,j,dis;
for(i=0;i<k;i++)
{
//strcpy(temp,bank[0]);
index=0;
for(j=0;j<k-i;j++)
{
dis=compare(j,index);
if(dis)
index=j;
}
swap(index,j-1);
}
}
int main()
{
int N,i,j;
while(1)
{
scanf("%d",&N);
if(N==0)
break;
for(i=0;i<N;i++)
scanf("%s",bank[i]);
sort(N);
for(i=0;i<N;i++)
printf("%s",bank[i]);
printf("\n");
}
return 0;
}
```