Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #define LEN 1234
  6. #define ASCII_LEN 256
  7.  
  8.  
  9. void clean_string(char *s, int n) {
  10.   memset(s, 0, n);
  11. }
  12.  
  13.  
  14. int solve(char **a, int n) {
  15.   int i, shifted_i, flag = 0;
  16.   char *c;
  17.   char hash[ASCII_LEN];
  18.  
  19.   // Нулевая итерация
  20.   clean_string(hash, ASCII_LEN);
  21.   for (c = a[0]; *c; c++) {
  22.     hash[(int) *c] = 1;
  23.   }
  24.  
  25.   // Идем по массиву
  26.   for (i = 1, shifted_i = 0; i < n; i++) {
  27.     // Проверяем совпадения букв через хеш таблицу
  28.     for (c = a[i]; *c; c++) {
  29.       if ((int)hash[(int) *c] == 1) {
  30.         flag = 1;
  31.         break;
  32.       }
  33.     }
  34.  
  35.     // Заполняем хеш-таблицу текущего элемента, сначала чистя его
  36.     clean_string(hash, ASCII_LEN);
  37.     for (c = a[i]; *c; c++) {
  38.       hash[(int) *c] = 1;
  39.     }
  40.  
  41.     // Если буквы не совпадали (flag == 0), то никаких пертираний не будет
  42.     if (flag == 0) {
  43.       shifted_i++;
  44.       a[shifted_i] = a[i];
  45.     }
  46.  
  47.     // Готовим переменные к след итерации
  48.     flag = 0;
  49.   }
  50.   shifted_i++;
  51.   a[shifted_i] = a[i];
  52.  
  53.   return shifted_i;
  54. }
  55.  
  56.  
  57. int main(void) {
  58.   int i, j, n;
  59.   const char * input = "input.txt";
  60.   const char * output = "output.txt";
  61.   FILE * fpin, * fpout;
  62.   char buf[LEN];
  63.   char ** a;
  64.  
  65.   if (!(fpin = fopen(input, "r"))) {
  66.     return -1;
  67.   }
  68.  
  69.   if (fscanf(fpin, "%d\n", & n) != 1) {
  70.     printf("Ошибка чтения\n");
  71.     return 1;
  72.   }
  73.  
  74.   a = (char ** ) malloc(n * sizeof(char * ));
  75.   if (!a) {
  76.     printf("Недостаточно памяти\n");
  77.     return 1;
  78.   }
  79.  
  80.   for (j = 0; j < n; j++) {
  81.     if (!fgets(buf, LEN, fpin))
  82.       break;
  83.  
  84.     for (i = 0; buf[i]; i++) {
  85.       if (buf[i] == '\n') {
  86.         buf[i] = 0;
  87.         break;
  88.       }
  89.     }
  90.     a[j] = (char * ) malloc(i * sizeof(char));
  91.     strcpy(a[j], buf);
  92.   }
  93.   if (!feof(fpin)) {
  94.     fclose(fpin);
  95.     return -3;
  96.   }
  97.   fclose(fpin);
  98.  
  99.   n = solve(a, n);
  100.  
  101.   if (!(fpout = fopen(output, "w"))) {
  102.     return -1;
  103.   }
  104.   for (j = 0; j < n; j++) {
  105.     fprintf(fpout, "%s", a[j]);
  106.     fprintf(fpout, "%s", "\n");
  107.   }
  108.   fclose(fpout);
  109.  
  110.   return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement