Advertisement
Guest User

Ne Sa si

a guest
Nov 22nd, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void prefix(char *buf){
  6. int size = strlen(buf);
  7. int j;
  8. int pi[size-1];
  9. for (int i = 0;i < size-1; i++){
  10. pi[i] = 0;
  11. }
  12. for (int i = 1;i < size-1; i++){
  13. j = pi[i-1];
  14. while ((j > 0) && (buf[i] != buf[j])){
  15. j = pi[j-1];
  16. }
  17.  
  18. if (buf[i] == buf[j]){
  19. pi[i] = j + 1;
  20. }
  21. else{
  22. pi[i] = j;
  23. }
  24. }
  25. printf("%d ", pi[size - 2]);
  26. }
  27.  
  28. int main(void){
  29. char buf1[1000002], buf2[1000002];
  30. fgets(buf1, 1000002, stdin);
  31. fgets(buf2, 1000002, stdin);
  32. int size1 = strlen(buf1);
  33. int size2 = strlen(buf2);
  34. int size = size1 + size2 - 1;
  35. char buf[size];
  36. for (int i = 0; i < size; i++){
  37. buf[i] = 0;
  38. }
  39. strcpy(buf, buf1);
  40. buf[size1 - 1] = '#';
  41. strcat(buf, buf2);
  42. prefix(buf);
  43. for (int i = 0; i < size; i++){
  44. buf[i] = 0;
  45. }
  46. strcpy(buf, buf2);
  47. buf[size2 - 1] = '#';
  48. strcat(buf, buf1);
  49. prefix(buf);
  50. return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement