Advertisement
Guest User

Untitled

a guest
May 26th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.08 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6.  
  7. FILE *input;
  8. FILE *output;
  9.  
  10. char myBuffer [256];
  11. int buffSize;
  12.  
  13. typedef struct knot{
  14.     unsigned long long mask;
  15.     int pos;
  16.     struct knot *child[36];
  17. }knot;
  18.  
  19. knot dictionary [5002];
  20.  
  21. int Hash(const char liter){
  22.     if ((liter >= 48)&&(liter<=57) ) return (int)(liter - 48);
  23.     return (int)(liter - 87);
  24. }
  25.  
  26. char BackHash(const int index){
  27.     if ((index >= 0)&&(index<=9) ) return (char)(index + 48);
  28.     return (char)(index + 87);
  29. }
  30.  
  31. void AddPref(const char liter,const int maxSize){
  32.     static int prefPos = 0;
  33.     static knot *nowKnot = &dictionary[0];
  34.  
  35.     int index = Hash(liter);
  36.     if ((nowKnot->mask) & (1ULL<<index)){
  37.         nowKnot = (nowKnot->child[index]);
  38.         return;
  39.     }
  40.     if (prefPos>=maxSize){
  41.         prefPos = 0;
  42.         nowKnot = &dictionary[0];
  43.         nowKnot->mask = 0;
  44.     }
  45.     nowKnot->mask |= (1ULL<<index);
  46.     nowKnot->child[index] = &dictionary[prefPos+1];
  47.     nowKnot->child[index]->mask = 0;
  48.     nowKnot->child[index]->pos = prefPos++;
  49.     nowKnot = &dictionary[0];
  50. }
  51.  
  52. void PrintPref(const knot *nowKnot){
  53.     static char buff [5001];
  54.     static int buffPos = 0;
  55.     for (int i = 0; i < 36;i++){
  56.         if ((nowKnot->mask)&(1ULL<<i)){
  57.             buff[buffPos++] = BackHash(i);
  58.             fprintf(output,"%s %i\n",buff,nowKnot->child[i]->pos);
  59.             PrintPref(nowKnot->child[i]);
  60.             buff[buffPos--] = 0;
  61.         }
  62.     }
  63. }
  64.  
  65.  
  66. int main() {
  67.  
  68.     input = fopen("input.txt", "r");
  69.     output = fopen("output.txt", "w");
  70.     int maxSize;
  71.     fscanf(input,"%i",&maxSize);
  72.     char liter;
  73.     while (!feof(input)){
  74.         buffSize = fread(myBuffer,1,256,input);
  75.         for (int i = 0; i<buffSize;++i) {
  76.             liter = tolower(myBuffer[i]);
  77.             if (((liter >= 97) && (liter <= 122))||((liter >= 48) && (liter <= 57))) {
  78.                 AddPref(liter, maxSize);
  79.             }
  80.         }
  81.     }
  82.  
  83.     PrintPref(&dictionary[0]);
  84.  
  85.     fclose(input);
  86.     fclose(output);
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement