Advertisement
mantertius

big_seq.c

Apr 13th, 2021
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.26 KB | None | 0 0
  1. //https://thehuxley.com/problem/862/oracle?quizId=6096
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #define DEBUG if (1)
  7. #define MAX 100001
  8. typedef struct node
  9. {
  10.     char item;
  11.     struct node *next;
  12. } NODE;
  13.  
  14. NODE *  create_list()
  15. {
  16.     return NULL;
  17. }
  18.  
  19. NODE *add(NODE *head, char item)
  20. {
  21.     NODE *new_node = (NODE *)malloc(sizeof(NODE));
  22.     new_node->item = item;
  23.     new_node->next = head;
  24.     //DEBUG printf("adicionado[[[%d]]]\t",new_node->item);
  25.     return new_node;
  26. }
  27.  
  28. int is_empty(NODE *head)
  29. {
  30.     return (head == NULL);
  31. }
  32.  
  33. void printer(NODE *head)
  34. {
  35.     if (head == NULL)
  36.     {
  37.         printf("Lista vazia!\n");
  38.     }
  39.     while (head != NULL)
  40.     {
  41.         printf("[%d]", head->item);
  42.         head = head->next;
  43.     }
  44.     printf("Fim da Lista!\n");
  45. }
  46.  
  47. NODE *search(NODE *head, char item)
  48. {
  49.     // printf("to dentro do search\n");
  50.     while (head != NULL)
  51.     {
  52.         if (head->item == item)
  53.         {
  54.             // printf("ENCONTREI MAIS UM!!! [%d]\n", item);
  55.             return head; //retorna o endereço que contém o item
  56.         }
  57.         head = head->next;
  58.     }
  59.     return NULL;
  60. }
  61.  
  62. int search_2(NODE *head, char item)//função para procurar a melhor sequencia
  63. {
  64.     DEBUG printf("######### Procurando por: [%c] ##########\n", item);
  65.     int count = 0;
  66.     while (head != NULL)
  67.     {
  68.         DEBUG printf("head->item = [%c]\t",head->item);
  69.         if (head->item == item)
  70.         {
  71.             count++;
  72.             //DEBUG printf("ENCONTREI MAIS UM!!! [%d]\n", item);
  73.             DEBUG printf("voltas: [%d]\n",count);
  74.             return count; //retorna o endereço que contém o item
  75.         }
  76.         head = head->next;
  77.         count++;
  78.         DEBUG printf("voltas: [%d]\n",count);
  79.     }
  80.     return 0;
  81. }
  82.  
  83. void func(NODE *head, int size, int beg, int end)//first_one, 1,
  84. {
  85.     NODE* first_zero = search(head,'0');//recebo o endereço do primeiro zero.
  86.     int beg_new = search_2(head,'0')+beg;//recebe a posição do primeiro zero
  87.     DEBUG printf("\tComeço encontrado: [%d]\n",beg_new);
  88.    
  89.     int size1 = search_2(first_zero,'1')-1; //a partir do endereço do primeiro zero, procuro o proximo 1 e recebo o tamanho deste intervalo
  90.     int end_new = beg_new+size1-1;//end é a posição final
  91.     DEBUG printf("\tFim encontrado: [%d]\n",end_new);
  92.     DEBUG printf("TAMANHO: [%d]\n",size1);
  93.     NODE* first_one = search(first_zero,'1'); //will be my new head
  94.     if(first_zero == NULL || first_one == NULL)//break
  95.     {
  96.         printf("%d %d\n",beg,end);
  97.         return;
  98.     }
  99.    
  100.     if (size1>size)
  101.     {
  102.         DEBUG printf("\n------- FUNC 1 --------\n");
  103.         func(first_one, size1, beg_new, end_new);
  104.     }
  105.     else{
  106.         DEBUG printf("\n------- FUNC 2 --------\n\n");
  107.         func(first_zero,size,beg,end);
  108.     }
  109.    
  110. }
  111. int main()
  112. {
  113.     char input[MAX];
  114.     NODE *lista1 = create_list(); //nó que aponta para nulo
  115.     do
  116.     {
  117.         scanf("%s", input);
  118.         int q = strlen(input);
  119.         if(q == 1 && input[0] == '0') break;
  120.         for (int i = q-1; i >= 0 ; i--)
  121.         {
  122.             //DEBUG printf("Adicionado [%c].\n",input[i]);
  123.             lista1 = add(lista1, input[i]);
  124.         }
  125.         func(lista1, 0,0,0);  
  126.     } while (1);
  127.  
  128.  
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement