Untitled
raw download clone
C
views 22
,
size 912 b
#include<stdio.h>
#include<string.h>
char s[1005][1006];
char* ptr[1005];


int compare(const void* a, const void* b)
{
 int ans;
 char** aa = (char**)a;
 //char* 是指到char的記憶體位置  char* *則是指到這個記憶體位置的記憶體位置
 char** bb = (char**)b;
 ans = strlen(*aa) - strlen(*bb);
 return ans;
}
int main()
{
 int n;
 char* zzz;
 int sign=0;
 scanf("%d", &n);
 for (int i = 0; i < n; i++) {
  scanf("%s", s[i]);
  ptr[i] = s[i];
 }
 qsort(ptr, n, sizeof(char*), compare);
 for (int i = 0; i < n - 1; i++) {
  zzz = strstr(ptr[i + 1], ptr[i]); //確認是否為substring
  if (zzz == NULL){ //如果有null則為NO
   sign = 1;
   break;
  }
 }


 if (sign == 0) { //利用SIGN來作為印出YES/NO的判別
  printf("YES\n");
  for (int i = 0; i < n; i++) {
   printf("%s\n", ptr[i]);
  }
 }
 else {
  printf("NO\n");
 }

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