fork download
  1. using System;
  2.  
  3. unsafe class Program
  4. {
  5. static int kmp(char[] substr, char[] str)
  6. {
  7. int i, j, N, M;
  8.  
  9. N = str.Length;
  10. M = substr.Length;
  11.  
  12. int*[] d = new int*[M * sizeof(int)];
  13. *d[0] = 0;
  14.  
  15. for (i = 0, j = 0; i < M; i++)
  16. {
  17. while (j > 0 && substr[j] != substr[i])
  18. {
  19. *d[j] = i - 1;
  20. }
  21.  
  22. if (substr[j] == substr[i])
  23. {
  24. j++;
  25. *d[i] = j;
  26. }
  27. }
  28.  
  29. for (i = 0, j = 0; i < N; i++)
  30. {
  31. while (j > 0 && substr[j] != str[i])
  32. {
  33. *d[j] = i - 1;
  34. }
  35.  
  36. if (substr[j] == str[i])
  37. {
  38. j++;
  39. }
  40.  
  41. if (j == M)
  42. {
  43. d = null;
  44. GC.Collect();
  45. return i - j + 1;
  46. }
  47. }
  48.  
  49. d = null;
  50. GC.Collect();
  51.  
  52. return -1;
  53. }
  54.  
  55. static void Main()
  56. {
  57. char[] substr = "World".ToCharArray(),
  58. str = "Hello World!\r\n".ToCharArray();
  59.  
  60. int pos = kmp(substr, str);
  61. }
  62. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty