KMP算法源码

 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4
  5 void getNextVal(char* T, int* nextVal);
  6 int IndexKMP(char* S, char* T);
  7 void getNextVal(char* T, int* nextVal)
  8 {
  9     int i = 1, j = 0;
 10     int nLen = strlen(T);
 11     printf("getNextVal-----nLen=%d\n", nLen);
 12     nextVal[0] = 0;
 13     while (i < nLen - 1)
 14     {
 15         if (T[i] == T[j])
 16         {
 17             ++i;
 18             ++j;
 19             if (T[i] != T[j])
 20             {
 21                 nextVal[i] = j;
 22             }
 23             else
 24             {
 25                 nextVal[i] = nextVal[j];
 26             }
 27         }
 28         else if (j == 0)
 29         {
 30             ++i;
 31             nextVal[i] = j;
 32         }
 33         else
 34         {
 35             j = nextVal[j];
 36         }
 37     }
 38 }
 39
 40 int IndexKMP(char* S, char* T)
 41 {
 42     int nLenT = strlen(T);
 43     int nLenS = strlen(S);
 44     int i = 0,j = 0;
 45     int* nextVal = (int*)malloc(sizeof(int) * nLenT);
 46     printf("IndexKMP----------nLenT=%d\n", nLenT);
 47     printf("IndexKMP----------nLenS=%d\n", nLenS);
 48     getNextVal(T, nextVal);
 49     
 50     while (i <= nLenS && j < nLenT)
 51     {
 52         if (S[i] == T[j])                                  
 53         {
 54             ++i;
 55             ++j;
 56         }
 57         else if (j == 0)
 58         {
 59             ++i;
 60         }
 61         else
 62         {
 63             j = nextVal[j];
 64         }
 65     }
 66
 67     if (j >= nLenT)
 68     {
 69         return i - nLenT;
 70     }
 71     else
 72     {
 73         return 0;
 74     }
 75 }
 76
 77 int main(int argc, char* argv[])
 78 {
 79     char* S = "ababcabcacbab";
 80     char* T = "abcac";
 81     printf("position = %d\n",IndexKMP(S,T));
 82     return 0;
 83 }
 84
 85


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是KMP算法的Java实现示例: ```java import java.util.ArrayList; import java.util.List; public class KMPAlgorithm { public static List<Integer> kmp(String text, String pattern) { List<Integer> positions = new ArrayList<>(); int n = text.length(); int m = pattern.length(); int[] lps = buildLPS(pattern); int i = 0; // text中的指针 int j = 0; // pattern中的指针 while (i < n) { if (text.charAt(i) == pattern.charAt(j)) { i++; j++; } if (j == m) { positions.add(i - j); j = lps[j - 1]; } else if (i < n && text.charAt(i) != pattern.charAt(j)) { if (j != 0) { j = lps[j - 1]; } else { i++; } } } return positions; } private static int[] buildLPS(String pattern) { int m = pattern.length(); int[] lps = new int[m]; int len = 0; // 最长公共前后缀的长度 lps[0] = 0; // 第一个字符没有最长公共前后缀 for (int i = 1; i < m; ) { if (pattern.charAt(i) == pattern.charAt(len)) { lps[i] = len + 1; len++; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } return lps; } public static void main(String[] args) { String text = "ABABDABACDABABCABAB"; String pattern = "ABABCABAB"; List<Integer> positions = kmp(text, pattern); if (positions.isEmpty()) { System.out.println("Pattern not found in the text."); } else { System.out.print("Pattern found at positions: "); for (int pos : positions) { System.out.print(pos + " "); } System.out.println(); } } } ``` 以上代码是KMP算法的Java实现示例,用于在文本串中查找模式串的出现位置。你可以将文本串和模式串替换为你自己的字符串进行测试。运行程序后,它会输出模式串在文本串中的出现位置。如果没有找到匹配的位置,则会输出"Pattern not found in the text."。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值