Advertisement
Guest User

Untitled

a guest
Nov 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <locale.h>
  5.  
  6. void merge(char *first, char *second, int second_len, int merge_pos, int curr);
  7.  
  8. void find_merge_position(char *first, char *second, int first_len, int second_len, int curr);
  9.  
  10. void parse_input(int curr);
  11.  
  12.  
  13. static char **result_strings;
  14.  
  15. int main() {
  16.     /// FUCK YOU
  17.     system("chcp 1251 > 1.txt");
  18.     int total;
  19.  
  20.     scanf_s("%i", &total);
  21.  
  22.     result_strings = (char **)malloc(sizeof(char *) * total);
  23.  
  24.     for (int i = 0; i < total; i++) {
  25.         parse_input(i);
  26.     }
  27.     for (int i = 0; i < total; i++) {
  28.         printf("%s\n", *(result_strings + i));
  29.     }
  30. }
  31.  
  32.  
  33. void find_merge_position(char *first, char *second, int first_len, int second_len, int curr) {
  34.     int res = first_len;
  35.     for (int i = 0; i < first_len; i++) {
  36.         // printf("%c, %c", first[i], second[0]);
  37.         if (first[i] == second[0]) {
  38.             int j = i;
  39.             while (first[j] == second[j - i]) {
  40.                 j++;
  41.                 if (j - i >= second_len) {
  42.                     // we came over second string length
  43.                     break;
  44.                 }
  45.             }
  46.             if (j == first_len) {
  47.                 res = i;
  48.                 break;
  49.             }
  50.         }
  51.     }
  52.     merge(first, second, second_len, res, curr);
  53. }
  54.  
  55.  
  56. void merge(char *first, char *second, int second_len, int merge_pos, int curr) {
  57.  
  58.     char *result = (char *)malloc(sizeof(char) * (second_len + merge_pos + 1));
  59.     for (int i = 0; i < merge_pos; i++) {
  60.         result[i] = first[i];
  61.     }
  62.     for (int i = 0; i < second_len; i++) {
  63.         result[i + merge_pos] = second[i];
  64.     }
  65.     result[merge_pos + second_len] = 0;
  66.     result_strings[curr] = result;
  67. }
  68.  
  69.  
  70. void parse_input(int curr) {
  71.     char *first = (char *)malloc(sizeof(char) * 1);
  72.     char *second = (char *)malloc(sizeof(char) * 1);
  73.     int first_length = 0, second_length = 0;
  74.  
  75.     char buffer = '1';
  76.     while (buffer != ' ') {
  77.         buffer = (char)getchar();
  78.         if (buffer == '\n') {
  79.             /**
  80.             * after first scanf "enter" key there is '\n' symbol in input.
  81.             * prevent reading it
  82.             */
  83.             continue;
  84.         }
  85.         // printf("%c, ", buffer);
  86.  
  87.         first = (char *)realloc(first, sizeof(char) * 1 + first_length);
  88.         *(first + first_length++) = buffer != ' ' ? buffer : 0;
  89.     }
  90.     while (buffer != '\n') {
  91.         buffer = (char)getchar();
  92.         second = (char *)realloc(second, sizeof(char) * 1 + second_length);
  93.         *(second + second_length++) = buffer != '\n' ? buffer : 0;
  94.     }
  95.     find_merge_position(first, second, first_length - 1, second_length - 1, curr);
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement