Advertisement
Guest User

zsndejvijpseopirtj

a guest
Nov 20th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. int char_comp(char c1, char c2) {
  7.     if (c1 == c2)
  8.         return 0;
  9.     if (islower(c1) && isupper(c2))
  10.         return -1;
  11.     if (isupper(c1) && islower(c2))
  12.         return 1;
  13.     if (c1 < c2)
  14.         return -1;
  15.     return 1;
  16. }
  17.  
  18. int comp(const void *s1, const void *s2) {
  19.     int i = 0;
  20.     while (1) {
  21.         char c1 = *(*(char **)(s1) + i);
  22.         char c2 = *(*(char **)(s2) + i);
  23.         if (!c1 || !c2)
  24.             break;
  25.         int cmp = char_comp(c1, c2);
  26.         if (cmp == 0) {
  27.             ++i;
  28.             continue;
  29.         }
  30.         return cmp;
  31.     }
  32.     char c1 = *(*(char **)(s1) + i);
  33.     char c2 = *(*(char **)(s2) + i);
  34.     if (!c1 && !c2)
  35.         return 0;
  36.     if (!c1)
  37.         return -1;
  38.     return 1;
  39. }
  40.  
  41. int main(void) {
  42.     int n;
  43.     scanf("%d\n", &n);
  44.  
  45.     char **a = malloc(n * sizeof(char*));
  46.  
  47.     for (int i = 0; i < n; ++i) {
  48.         int c;
  49.         char *s = malloc(1);
  50.         int sz = 0, mem_sz = 1;
  51.  
  52.         while (1) {
  53.             c = getchar();
  54.             if (c == '\n' || c == EOF)
  55.                 break;
  56.             if (sz + 1 >= mem_sz) {
  57.                 mem_sz *= 2;
  58.                 char *s0 = realloc(s, mem_sz);
  59.                 s = s0;
  60.             }
  61.             s[sz] = c;
  62.             ++sz;
  63.         }
  64.  
  65.         if (sz + 1 >= mem_sz) {
  66.             mem_sz += 1;
  67.             char *s0 = realloc(s, mem_sz);
  68.             s = s0;
  69.         }
  70.         s[sz] = '\0';
  71.         a[i] = realloc(s, sz + 1);
  72.     }
  73.  
  74.     qsort(a, n, sizeof(char*), comp);
  75.  
  76.     for (int i = 0; i < n; ++i) {
  77.         printf("%s\n", a[i]);
  78.     }
  79.     free(a);
  80.  
  81.     return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement