Guest User

Untitled

a guest
Apr 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.78 KB | None | 0 0
  1. // ads1.cpp : Defines the entry point for the console application.
  2.  
  3. #include "stdafx.h"
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8.  
  9. typedef struct _string
  10. {
  11.     int length;
  12.     char value[100010];
  13. }string;
  14.  
  15. typedef struct _vertex
  16. {
  17.     int length;
  18.     struct _vertex *back, *shortcut, *children[63];
  19.     char value;
  20.     int is_end : 2;
  21. }vertex;
  22.  
  23. vertex *tree;
  24.  
  25. string *read_line(FILE *f)
  26. {
  27.     string *s = (string*)malloc(sizeof(string));
  28.     if(fgets(s->value, 100005, f) == NULL) return NULL;
  29.     s->length = strlen(s->value)-1;
  30.  
  31.     printf("%c %d\n", s->value[s->length - 1], s->length);
  32.     //int momo; scanf("%d", &momo);
  33.     return s;
  34. }
  35.  
  36. void add_word(string *s)
  37. {
  38.     int i;
  39.     vertex *tmp = tree;
  40.  
  41.     for(i = 0; i < s->length; ++i)
  42.     {
  43.         if(s->value[i] < 1) return;
  44.         //printf("<%d>\n", s->value[i]);
  45.         int index = 0;
  46.         if(isdigit(s->value[i]))
  47.         {
  48.             index = s->value[i] - 48;
  49.         }
  50.         if(isupper(s->value[i]))
  51.         {
  52.             index = s->value[i] - 65 + 10;
  53.         }
  54.         if(islower(s->value[i]))
  55.         {
  56.             index = s->value[i] - 97 + 10 + 26;
  57.         }
  58.  
  59.         if(tmp->children[index] == NULL)
  60.         {
  61.             tmp->children[index] = (vertex*)malloc(sizeof(vertex));
  62.             int i;
  63.             for(i = 0; i < 63; i++) tmp->children[index]->children[i] = NULL;
  64.             tmp->children[index]->back = NULL;
  65.             tmp->children[index]->shortcut = NULL;
  66.             tmp->children[index]->value = s->value[i];
  67.             tmp->children[index]->is_end = 0;
  68.         }
  69.  
  70.         tmp = tmp->children[index];
  71.     }
  72.  
  73.     tmp->is_end = 1;
  74.     tmp->length = s->length;
  75. }
  76.  
  77. int main(int argc, char** argv)
  78. {
  79.     tree = (vertex*)malloc(sizeof(vertex));
  80.     tree->value = ' ';
  81.     int i;
  82.     for(i = 0; i < 63; i++)tree->children[i] = NULL;
  83.     tree->back = NULL;
  84.     tree->shortcut = NULL;
  85.     tree->is_end = 0;
  86.     tree->length = 0;
  87.  
  88.  
  89.     string* text = read_line(stdin);
  90.     while(1)
  91.     {
  92.         string *line = read_line(stdin);
  93.         if(line == NULL) break;
  94.  
  95.         add_word(line);
  96.     }
  97.  
  98.     //printf("add word se povedli\n");
  99.  
  100.     vertex *stack[100000];
  101.     int stack_pointer = -1;
  102. //  int i;
  103.     for(i = 0; i < 63; ++i)
  104.     {
  105.         if(tree->children[i] != NULL)
  106.         {
  107.             if(tree->children[i]->value < 1)continue;
  108.             tree->children[i]->back = tree;
  109.             stack[++stack_pointer] = tree->children[i];
  110.  
  111.             //printf("pridal som do stacku, na poziciu %d tento prvok.value <%d>\n", stack_pointer, tree->children[i]->value);
  112.         }
  113.     }
  114.  
  115.     while(stack_pointer >= 0)
  116.     {
  117.             vertex *v = stack[stack_pointer--];
  118.             //printf("vzal som zo stacku, z pozicie %d tento prvok.value <%d>\n", stack_pointer+1, v->value);
  119.             int i;
  120.             for(i = 0; i < 63; i++)
  121.             {
  122.                 if(v->children[i] != NULL)
  123.                 {
  124.                     stack[++stack_pointer] = v->children[i];
  125.                    
  126.                     if(v->children[i]->value < 1)continue;
  127.                     int index = 0;
  128.                     //printf("<%d>\n", v->childre);
  129.                     if(isdigit(v->children[i]->value))
  130.                     {
  131.                         index = v->children[i]->value - 48;
  132.                     }
  133.                     if(isupper(v->children[i]->value))
  134.                     {
  135.                         index = v->children[i]->value - 65 + 10;
  136.                     }
  137.                     if(islower(v->children[i]->value))
  138.                     {
  139.                         index = v->children[i]->value - 97 + 10 + 26;
  140.                     }
  141.  
  142.                     vertex *n = v->back;
  143.                     while(n != tree && n->children[index] == NULL) n = n->back;
  144.                     if(n->children[index] != NULL) n = n->children[index];
  145.                     v->children[i]->back = n;
  146.  
  147.                     if(v->children[i]->back->back != NULL && v->children[i]->back->back != tree) v->children[i]->back = v->children[i]->back->back;
  148.  
  149.                     if(v->children[i]->is_end == 1)
  150.                     {
  151.                         v->children[i]->shortcut = v->children[i]->back;
  152.                         if(v->children[i]->back->shortcut != NULL) v->children[i]->shortcut = v->children[i]->back->shortcut;
  153.                     }
  154.                     else v->children[i]->shortcut = v->children[i]->shortcut;
  155.                 }
  156.  
  157.             }
  158.     }
  159.  
  160.  
  161.     vertex *a = tree;
  162.     vertex *b = NULL;
  163.  
  164.     char znaky[100001];
  165.     vertex *stavy[100001];
  166.     int indexZnak = 0;
  167.     int indexStav = 0;
  168.     int k;
  169.     for(k = 0; k < text->length; k++)
  170.     {
  171.         int index = 0;
  172.         //printf("<%d>\n", v->childre);
  173.         if(isdigit(text->value[k]))
  174.         {
  175.             index = text->value[k] - 48;
  176.         }
  177.         if(isupper(text->value[k]))
  178.         {
  179.             index = text->value[k] - 65 + 10;
  180.         }
  181.         if(islower(text->value[k]))
  182.         {
  183.             index = text->value[k] - 97 + 10 + 26;
  184.         }
  185.  
  186.         vertex *n = a;
  187.         while(n->children[index] == NULL && n != tree)
  188.         {
  189.             n = n->back;
  190.         }
  191.         if(n->children[index] != NULL) n = n->children[index];
  192.  
  193.         a = n;
  194.         b = a;
  195.  
  196.         znaky[indexZnak] = text->value[k];
  197.         indexZnak++;
  198.  
  199.         stavy[indexStav] = b;
  200.         indexStav++;
  201.  
  202.         if(b->is_end == 1)
  203.         {
  204.             if(b->shortcut != NULL)
  205.             {
  206.                 b = b->shortcut;
  207.             }
  208.  
  209.             indexZnak -= b->length;
  210.             indexStav -= b->length;
  211.             //printf("odpocitavam %d\n", b->length);
  212.  
  213.             if(indexStav == 0) a = tree;
  214.             else a = stavy[indexStav - 1];
  215.         }
  216.     }
  217.  
  218.     int momo;
  219.     for(momo = 0; momo < indexZnak; momo++)
  220.     {
  221.         printf("%c", znaky[momo]);
  222.     }
  223.     printf("\n");
  224.     ;
  225.     //scanf("%d", &momo);
  226.    
  227.  
  228.     return 0;
  229. }
Add Comment
Please, Sign In to add comment