Untitled
raw download clone
C
views 14
,
size 1618 b
#include<stdio.h>//先照字串長度排列,再檢查YES/NO
#include<stdlib.h>
#include<string.h>

int n;
char s[1001][1001];

int main(void)
{
     int i,j,k,flag;
     int *len,*pos;
     scanf("%d\n",&n);
     len=(int*)malloc(n*sizeof(int));
     pos=(int*)malloc(n*sizeof(int));
     for(i=0,j=0;i<n;i++,j=0)
     {
          while(scanf("%c",&s[i][j]))
          {
               if(s[i][j]=='\n')
               {
                    s[i][j]='\0';
                    len[i]=j;
                    break;
               }
               j++;
          }
          //len[i]=strlen(s[i]);
          pos[i]=i;
     }
     for(i=1;i<n;i++)//insertion sort
     {
          int tmp=pos[i];
          j=i;
          while(j>0&&len[pos[j-1]]>len[tmp])
          {
               pos[j]=pos[j-1];
               j--;
          }
          pos[j]=tmp;
     }
     for(i=1;i<n;i++)
     {
          for(j=0,flag=0;j<len[pos[i]];j++)
          {
               for(k=0;k<len[pos[i-1]];k++)
               {
                    if(s[pos[i]][j+k]!=s[pos[i-1]][k])break;
               }
               if(k==len[pos[i-1]])
               {
                    flag=1;
                    //printf("i=%d\n",i);
                    break;
               }
          }
          if(flag==0)break;
     }
     if(i!=n)
     {
          printf("NO\n");
     }
     else
     {
          printf("YES\n");
          for(i=0;i<n;i++)
          {
               printf("%s\n",s[pos[i]]);
          }
     }

     free(len);
     free(pos);
     return 0;
}
close fullscreen
Login or Register to edit or fork this paste. It's free.