Advertisement
sildren12

Untitled

Apr 2nd, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.53 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int dlugosc (char *lan);
  4. int *init_next (char *wzor);
  5. void init_shift(char *w, int *shift);
  6. int KMP(char *tekst, char *wzor, int *next);
  7. int BM(char *tekst, char *wzor);
  8. int *KMP_w(char *tekst, char *wzor, int *next, int *ilosc);
  9. int *BM_w(char *tekst, char *wzor, int *ilosc);
  10.  
  11. main()
  12. {
  13.     char wzor[20], tekst[200];
  14.     int i, N, M, ilosc=0, *next, *indeksy, pos=0;
  15.     indeksy=NULL;
  16.  
  17.     while (pos!=-1)
  18.     {
  19.         printf("\n----K M P----\n");
  20.         printf("\nPodaj tekst:");
  21.         gets(tekst);
  22.         printf("\nPodaj wzorzec:");
  23.         fflush(stdin);
  24.         gets(wzor);
  25.         //N=dlugosc(tekst);
  26.         //M=dlugosc(wzor);
  27.         next=init_next(wzor);
  28.         pos=KMP(tekst, wzor, next);
  29.         if (pos>=0)
  30.         {
  31.             printf("\nZnaleziono wzorzec na pozycji: %d", pos);
  32.             indeksy=KMP_w(tekst, wzor, next, &ilosc);
  33.             printf("\nIlosc powtorzen wzorca: %d", ilosc);
  34.             printf("\nna pozycjach: \n");
  35.             for (i=0; i<ilosc; i++)
  36.                 printf("%d; ", *(indeksy+i));
  37.         }
  38.         else
  39.             printf("\nNie znaleziono wzorca w tekscie\n");
  40.         getch();
  41.     }
  42.     pos = 0;
  43.     while (pos!=-1)
  44.     {
  45.         printf("\n----B M----\n");
  46.         printf("\nPodaj tekst:");
  47.         gets(tekst);
  48.         printf("\nPodaj wzorzec:");
  49.         fflush(stdin);
  50.         gets(wzor);
  51.         //N=dlugosc(tekst);
  52.         //M=dlugosc(wzor);
  53.         pos=BM(tekst, wzor);
  54.         if (pos>=0)
  55.         {
  56.             printf("\nZnaleziono wzorzec na pozycji: %d", pos);
  57.             indeksy=BM_w(tekst, wzor, &ilosc);
  58.             printf("\nIlosc powtorzen wzorca: %d", ilosc);
  59.             printf("\nna pozycjach: \n");
  60.             for (i=0; i<ilosc; i++)
  61.                 printf("%d; ", *(indeksy+i));
  62.         }
  63.         else
  64.             printf("\nNie znaleziono wzorca w tekscie\n");
  65.         getch();
  66.     }
  67. }
  68.  
  69. int dlugosc(char *lan)
  70. {
  71.     int i=0;
  72.     while(*(lan+i)!='\0') i++;
  73.     return i;
  74. }
  75.  
  76. int *init_next(char *wzor)
  77. {
  78.     int i, j;
  79.     int M=dlugosc(wzor);
  80.     int *next;
  81.     next = malloc(M*sizeof(int));
  82.     *next=-1;
  83.     for(i=0, j=-1; i<M-1; i++, j++, *(next+i)=j)
  84.     {
  85.         while ((j>=0) && (wzor[i] != wzor[j])) j=*(next+j);
  86.     }
  87.     return next;
  88. }
  89.  
  90. void init_shift(char *w, int *shift)
  91. {
  92.     int i, K=256, M=dlugosc(w);
  93.     for (i=0; i<K; i++)
  94.         *(shift+i)=M;
  95.     for (i=0; i<M; i++)
  96.         *(shift+ *(w+i))=M-i-1;
  97. }
  98.  
  99. int KMP(char *tekst, char *wzor, int *next)
  100. {
  101.     int i, j, N=dlugosc(tekst), M=dlugosc(wzor);
  102.     for (i=0, j=0; i<N && j<M; i++, j++)
  103.         while((j>=0)&&(tekst[i] != wzor[j])) j=next[j];
  104.     if (j==M) return i-M;
  105.     else return -1;
  106. }
  107.  
  108. int BM(char *tekst, char *wzor)
  109. {
  110.     int shift[256], M=dlugosc(wzor), N=dlugosc(tekst), x, i, j;
  111.     for (i=M-1, j=M-i; j>=0; i--, j--)
  112.         while (tekst[i] != wzor[j])
  113.         {
  114.             x=shift[tekst[i]];
  115.             if (M-j >x) i+=M-j;
  116.             else i+=x;
  117.             if (i>=N) return -1;
  118.             j=M-1;
  119.         }
  120.         return i+1;
  121. }
  122.  
  123. int *KMP_w(char *tekst, char *wzor, int *next, int *ilosc)
  124. {
  125.     int i=0, j, N=dlugosc(tekst), M=dlugosc(wzor), k=0;
  126.     int *indeksy;
  127.     indeksy=NULL;
  128.     *ilosc =0;
  129.     while (k<N)
  130.     {
  131.         for (i=k, j=0; i<N && j<M; i++, j++)
  132.             while ((j>=0)&&(tekst[i] != wzor[j]))
  133.                 j=next[j];
  134.         if(j==M)
  135.         {
  136.             indeksy = (int*)realloc(indeksy, (*ilosc+1)*sizeof(int));
  137.             *(indeksy+ *ilosc)=i-M;
  138.             (*ilosc)+=1;;
  139.         }
  140.         k=i;
  141.     }
  142.     return indeksy;
  143. }
  144.  
  145. int *BM_w(char *tekst, char *wzor, int *ilosc)
  146. {
  147.     int shift[256], N=dlugosc(tekst), M=dlugosc(wzor), x, i, j, k;
  148.     int *indeksy=NULL;
  149.     init_shift(wzor, shift);
  150.     *ilosc=0;
  151.     k=M-1;
  152.     while(k<N)
  153.     {
  154.         for (i=k, j=M-1; j>=0; i--, j--)
  155.             while(tekst[i] != wzor[j])
  156.             {
  157.                 x=shift[tekst[i]];
  158.                 if (M-j>x) i+=M-j;
  159.                 else i+=x;
  160.                 if(i>=N) return indeksy;
  161.                 j=M-1;
  162.             }
  163.             *ilosc+=1;
  164.             indeksy=(int*)realloc(indeksy, (*ilosc)*sizeof(int));
  165.             *(indeksy+(*ilosc)-1)=i+1;
  166.             k=i-j+M;
  167.     }
  168.     return indeksy;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement