Advertisement
poor_fool_mloody

babcia c

Apr 16th, 2017
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. int dlugosc(char*lan);
  5. int *init_next(char*wzor);
  6. int KMP(char*tekst,char*wzor,int*next);
  7. void init_shift(char*wzor,int*shift);
  8. //int BM(char *tekst,char*wzor);
  9.  
  10. main()
  11. {
  12. int N,M,P,L,*i=NULL;
  13. char lancuch[100];
  14. char wzorzec[30];
  15. printf("Podaj lancuch: \n");
  16. fflush(stdin);
  17. scanf("%99[^\n]s",lancuch);
  18. printf("Podaj wzorzec: \n");
  19. fflush(stdin);
  20. scanf("%29[^\n]s",wzorzec);
  21. i=init_next(wzorzec);
  22. printf("%d", i[5]);
  23. P=KMP(lancuch,wzorzec,i);
  24. if(P!=-1)
  25. printf("Znaleziono wzorzec w tekscie.\nZnajduje sie on w indeksie %d\n",P+1);
  26. else
  27. printf("Nie znaleziono wzorca.\n");
  28. //L=BM(lancuch,wzorzec);
  29. //if(L!=-1)
  30. //printf("Znaleziono wzorzec w tekscie.\nZnajduje sie on w indeksie %d\n",L);
  31. //else
  32. //printf("Nie znaleziono wzorca.\n");
  33. //printf("Podany lancuch ma dlugosc %d\n",N);
  34. system("pause");
  35. getchar();
  36. return(0);
  37. }
  38.  
  39. int dlugosc(char*lan)
  40. {
  41. int i=0;
  42. int d=0;
  43. while(lan[i]!='\0')
  44. {
  45. d=d+1;
  46. i++;
  47. }
  48. return d;
  49. }
  50. int *init_next(char*wzor)
  51. {
  52. int *next=NULL;
  53. int i,j,M;
  54. M=dlugosc(wzor);
  55.  
  56. next=(int*)malloc(M*sizeof(int));
  57. next[0]=-1;
  58. for(i=0,j=-1;i<M-1;i++,j++,next[i]=j){
  59.  
  60. while((j>=0)&&(wzor[i]!=wzor[j])){
  61.  
  62. j=next[j];
  63.  
  64. }
  65.  
  66. }
  67.  
  68. return next;
  69. }
  70. int KMP(char*tekst,char*wzor,int *next)
  71. {
  72.  
  73. int i,j,N,M;
  74. N=dlugosc(tekst);
  75. M=dlugosc(wzor);
  76.  
  77. for(i=0,j=0;i<N&&j<M;i++,j++)
  78. while((j>=0)&&(tekst[i]!=wzor[j]))
  79. j=next[j];
  80. if(j==M)
  81. return i-M;
  82. else
  83. return -1;
  84. }
  85. void init_shift(char*wzor,int*shift)
  86. {
  87. int K=256,i,M;
  88. M=dlugosc(wzor);
  89. for(i=0;i<K;i++)
  90. shift[i]=M;
  91. for(i=0;i<M;i++)
  92. shift[wzor[i]]=M-i-1;
  93.  
  94. }
  95. //int BM(char*tekst,char*wzor)
  96. //{
  97. // int i,j,x,M,N;
  98. // N=dlugosc(tekst);
  99. // M=dlugosc(wzor);
  100. // int *shift=NULL;
  101. // if(M>N)
  102. // {
  103. // return -1;
  104. // }
  105. // for(i=M-1,j=M-1;j>=0;i--,j--)
  106. // while(tekst[i]!=wzor[j])
  107. // {
  108. // x=shift[tekst[i]];
  109. // if(M-j>x)
  110. // i+=M-j;
  111. // else
  112. // i+=x;
  113. // if(i>=N)
  114. // {
  115. // return -1;
  116. // }
  117. // j=M-1;
  118. // }
  119. // return i+1;
  120. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement