KMP算法(java、C#)

文章目录

kmp中的nextVal(代码用next数组表示)

cs 复制代码
namespace Testmain
{
    public class GetNext
    {
        int[] next;
        public int[] getNextArray(char[] ch)
        {
            next = new int[ch.Length];
            int i = 0, j = -1;
            next[0] = -1;
            while (i < ch.Length - 1)
            {
                if (j == -1 || ch[i] == ch[j])
                {
                    i++;
                    j++;
                    if (ch[i] != ch[j])
                    {
                        next[i] = j;
                    }
                    else
                    {
                        next[i] = next[j];
                    }
                }
                else
                {
                    j = next[j];
                }
            }
            return next;
        }
    }
}

获取匹配成功的主串下标

cs 复制代码
namespace Testmain
{
    public class Index_KMP
    {
        public int getIndex(char[] S, char[] T)
        {
            int i = 0;//main string
            int j = 0;//model string
            int[] next = new int[255];
            GetNext getNext = new GetNext();
            next = getNext.getNextArray(T);
            while (i <= S.Length - 1 && j <= T.Length - 1)
            {
                if (j == -1 || S[i] == T[j])
                {
                    ++i;
                    ++j;
                }
                else
                {
                    j = next[j];
                }
            }
            if (j > T.Length - 1)
            {
                return i - j;
            }
            else
            {
                return 0;
            }
        }
    }
}

程序入口(示例)

cs 复制代码
using System;
namespace Testmain
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                char[] S = { 'w', 'a', 'b', 'a', 'b', 'a', 'b', 'a', 'a', 'a', 'b', 'a', 'm' };
                char[] chars = { 'a', 'b', 'a', 'b', 'a', 'a', 'a', 'b', 'a' };
                Index_KMP index_KMP = new Index_KMP();
                var A = index_KMP.getIndex(S, chars);
                Console.WriteLine(A);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }

    }
}
相关推荐
IT枫斗者9 小时前
Netty的原理和springboot项目整合
java·spring boot·后端·sql·科技·mysql·spring
Keep_Trying_Go9 小时前
统一的人群计数训练框架(PyTorch)——基于主流的密度图模型训练框架
人工智能·pytorch·python·深度学习·算法·机器学习·人群计数
(●—●)橘子……9 小时前
记力扣557.反转字符串中的单词 练习理解
算法·leetcode·职场和发展
Edward111111119 小时前
普通java项目转为maven项目 J文件后缀.java变C文件
java·开发语言·maven
YJlio9 小时前
BgInfo 学习笔记(11.5):多种输出方式(壁纸 / 剪贴板 / 文件)与“更新其他桌面”实战
笔记·学习·c#
Zhen (Evan) Wang9 小时前
.NET 6 API使用Serilog APM
c#·.net
一雨方知深秋9 小时前
二.java程序基本语法
java·类型转换·变量·方法·运算符·字面量·关键字标识符
啊阿狸不会拉杆9 小时前
《数字图像处理 》 第 1 章-绪论
图像处理·python·opencv·算法·数字图像处理
Java程序之猿9 小时前
Springboot 集成apache-camel +mqtt 根据主题处理mqtt消息
java·spring boot·后端
智驱力人工智能9 小时前
加油站静电夹检测 视觉分析技术的安全赋能与实践 静电夹检测 加油站静电夹状态监测 静电接地报警器检测
人工智能·深度学习·算法·安全·yolo·边缘计算