Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define SIZE 1000000
  5.  
  6. int ans(char *str1, char *str2, int *p)
  7. {
  8. if (str1[0] == str2[0])
  9. p[0] = 1;
  10. int k, i;
  11. for (i = 1; i < strlen(str2); ++i)
  12. {
  13. k = p[i - 1];
  14. while (k > 0 && str1[k] != str2[i])
  15. k = p[k - 1];
  16. if (str1[k] == str2[i])
  17. ++k;
  18. p[i] = k;
  19. }
  20. return p[i - 1];
  21. }
  22.  
  23. int main(void)
  24. {
  25. static char str1[SIZE + 1];
  26. static char str2[SIZE + 1];
  27. scanf("%1000000s%1000000s", str1, str2);
  28.  
  29. static int p1[SIZE] = { 0 };
  30. static int p2[SIZE] = { 0 };
  31.  
  32. printf("%d ", ans(str1, str2, p1));
  33. printf("%d", ans(str2, str1, p2));
  34.  
  35. return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement