Java Implementation KMP algorithm

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/tkd03072010/article/details/6824326

——————————————————————————————————

  1. Package arithmetic;
  2. /**
  3. * Java Implementation KMP algorithm
  4. *
  5. * Thought: Every time a trip to match the occurrence of character comparisons, do not need to backtrack I pointer,
  6. * Instead, use the result of the "partial match" that has been obtained to "swipe" the pattern to the right as far as possible
  7. * After a certain distance, continue to compare.
  8. *
  9. * Time complexity O (n+m)
  10. *
  11. * @author XQH
  12. *
  13. */
  14. Public class Kmptest {
  15. public static void Main (string[] args) {
  16. String s = "Abbabbbbcab"; //main string
  17. String t = "Bbcab"; //mode string
  18. char[] ss = S.tochararray ();
  19. char[] tt = T.tochararray ();
  20. System.out.println (Kmp_index (SS, TT)); //KMP Match string
  21. }
  22. /** 
  23. * Get the next function value of the string
  24. *
  25. * @param t
  26. * String
  27. * @return Next function value
  28. */
  29. public static int[] Next (char[] t) {
  30. int[] Next = new int[t.length];
  31. next[0] =-1;
  32. int i = 0;
  33. Int J =-1;
  34. While (i < t.length- 1) {
  35. if (j = =-1 | | t[i] = = T[j]) {
  36. i++;
  37. j + +;
  38. if (t[i]! = T[j]) {
  39. Next[i] = j;
  40. } Else {
  41. Next[i] = Next[j];
  42. }
  43. } Else {
  44. j = Next[j];
  45. }
  46. }
  47. return next;
  48. }
  49. /** 
  50. * KMP Match string
  51. *
  52. * @param s
  53. * Main string
  54. * @param t
  55. * Mode string
  56. * @return If the match succeeds, return subscript, otherwise return-1
  57. */
  58. public static int Kmp_index (char[] s, char[] t) {
  59. int[] Next = next (t);
  60. int i = 0;
  61. Int J = 0;
  62. While (i <= s.length- 1 && J <= t.length- 1) {
  63. if (j = =-1 | | s[i] = = T[j]) {
  64. i++;
  65. j + +;
  66. } Else {
  67. j = Next[j];
  68. }
  69. }
  70. if (J < t.length) {
  71. return-1;
  72. } Else
  73. return i-t.length; //Return the head subscript of the pattern string in the main string
  74. }
  75. }

Java Implementation KMP algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.