Advertisement
bartek27210

algorytmy lab2

Mar 29th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int dlugosc(char *lan);
  5. int* init_next(char *wzor);
  6. int KMP(char *tekst, char *wzor, int *next);
  7. int* KMP_w(char* tekst, char* wzor, int* next, int* ilosc);
  8.  
  9. int main()
  10. {
  11.     char string[50];
  12.     char pattern[50];
  13.     int *next, index, *table_of_index, counter=0, i;
  14.  
  15.     do
  16.     {
  17.     printf("\nPodaj lancuch: ");
  18.     fflush(stdin);
  19.     scanf("%s", string);
  20.  
  21.     printf("\nPodaj wzor: ");
  22.     fflush(stdin);
  23.     scanf("%s", pattern);
  24.  
  25.     next=init_next(pattern);
  26.     index=KMP(string, pattern, next);
  27.  
  28.     if(index==-1)
  29.         printf("Nie znaleziono wzorca w tekscie!");
  30.     else
  31.         {
  32.             printf("Wzorzec na pozycji: %d \n",index);
  33.             table_of_index=KMP_w(string,pattern,next,&counter);
  34.             printf("\nZnaleziono: %d wzorow\n",counter);
  35.             if(counter>1)
  36.             {
  37.                 for(int i=0; i<counter; i++)
  38.                 {
  39.                     printf("\n%d",table_of_index[i]);
  40.                 }
  41.             }
  42.             else
  43.             {
  44.                 printf("Znaleziono tylko jeden wzor\n");
  45.             }
  46.         }
  47.     }while(index != -1);
  48.    return 0;
  49. }
  50.  
  51. int dlugosc(char *lan)
  52. {
  53.     int length=0;
  54.     while(lan[length]!='\0')
  55.     {
  56.         length++;
  57.     }
  58.     return length;
  59. }
  60.  
  61. int *init_next(char *wzor)
  62. {
  63.     int *next=NULL;
  64.     int i, j, p_length=dlugosc(wzor);
  65.  
  66.     for(i=0, j=-1; i<p_length; i++, j++)
  67.     {
  68.         next=(int*)realloc(next, (i+1)*sizeof(int));
  69.         next[i]=j;
  70.  
  71.         while((j>=0)&&(wzor[i]!=wzor[j]))
  72.         j=next[j];
  73.     }
  74.     return next;
  75. }
  76.  
  77. int KMP(char *tekst, char *wzor, int* next)
  78. {
  79.     int i=0, j=0, p_length=dlugosc(wzor), s_length=dlugosc(tekst);
  80.  
  81.     for (i=0, j=0; j<p_length &&i<s_length; i++,j++)
  82.     {
  83.         while((j>=0) && (tekst[i]!=wzor[j]))
  84.             j=next[j];
  85.     }
  86.     if(j==p_length)
  87.     return i-p_length+1;
  88.     else
  89.     return -1;
  90. }
  91.  
  92. int* KMP_w(char* tekst, char* wzor, int* next, int* ilosc)
  93. {
  94.     int i=0, j=0, p_length=dlugosc(wzor), s_length=dlugosc(tekst);
  95.     int* table_of_index=NULL;
  96.  
  97.     while(i<=s_length-p_length)
  98.     {
  99.         for (i, j=0; j<p_length &&i<s_length; i++,j++)
  100.         {
  101.             while((j>=0) && (tekst[i]!=wzor[j]))
  102.                 j=next[j];
  103.         }
  104.  
  105.         if(j==p_length)
  106.         {
  107.             *ilosc+=1;
  108.             table_of_index=(int*)realloc(table_of_index,(*ilosc)*sizeof(int));
  109.             table_of_index[*ilosc-1]=i-p_length+1;
  110.             i=i-p_length+1;
  111.         }
  112.         else
  113.             break;
  114.     }
  115.     return table_of_index;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement