首页 > 编程语言 >JAVA KMP 纯模板

JAVA KMP 纯模板

时间:2024-05-17 15:41:02浏览次数:14  
标签:JAVA int s2 s1 next ++ static KMP 模板

纯模板 记忆使用~

class Main {

    static char[] s1;
    static char[] s2;
    static int[] next;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        s1 = in.nextLine().toCharArray();
        s2 = in.nextLine().toCharArray();
        int m = s2.length;
        int n = s1.length;
        next = new int[m + 1];
        getNext();
        //System.out.println(Arrays.toString(next));
        List<Integer> res = new ArrayList<>();
        int i = 0, j = 0;
        while(i < n){
            if(j == -1 || s1[i] == s2[j]){
                i++;
                j++;
            }else{
                j = next[j];
            }
            if(j == m){//可重复匹配版本 单个匹配直接break
                res.add(i - j);
                i = i - j + 1;
                j = 0;

            }
        }
        for(int re: res)
            System.out.println(re + 1);

    }

    static void getNext(){
        next[0] = -1;
        int m = s2.length;

        int k = -1;
        int j = 0;
        while(j < m){
            if(k == -1 || s2[k] == s2[j]){
                ++k;
                ++j;
                next[j] = k;
            }else{
                k = next[k];
            }
        }
    }

}

标签:JAVA,int,s2,s1,next,++,static,KMP,模板
From: https://www.cnblogs.com/ganyq/p/18197888

相关文章

  • [Javascript] Find Items from the end of the JavaScript Array using at, findLast
    Findingelementsstartingfromtheendofanarrayhasgottenaloteasierwiththeintroductionofthe at, findLast,and findLastIndex methods!With at younolongerneedtoremembertoaccesstheendofthearraylike array[array.length-1] trick.......
  • [Javascript] Object.groupBy & Map.groupBy
    ArrayGrouping isthenewfeatureofJavaScript/ECMAScript,whichsplitsanarray(or,generally,aniterable),intosmallersub-arrays.GroupingisdifferentthanotherJSarraymethods-it's not apartofthearrayprototype,butastaticmethod.......
  • Java(6)-Java内存区域和作用
    本文在终于搞懂了java8的内存结构,再也不纠结方法区和常量池了!_java8堆中存放静态变量和字符串常量池吗-CSDN博客基础上加入了一些个人思考,原文写得就很通俗易懂,推荐Java内存Java程序在运行过程中使用的内存可以分成虚拟内存和本地内存两大类。虚拟内存虚拟内存,就是指JVM自己管......
  • 多线程下使用List中的subList和remove方法产生的 java.util.ConcurrentModificationEx
    在说多线程操作List之前,我们先看下单线程下产生的问题:单线程List<Integer>listA=newArrayList<>();listA.add(1);listA.add(2);listA.add(3);listA.add(4);listA.add(5);listA.add(6);for(Integera:listA){......
  • java netty 实现 websocket 服务端和客户端双向通信 实现心跳和断线重连 完整示例
    javanetty实现websocket服务端和客户端双向通信实现心跳和断线重连完整示例maven依赖<dependency><groupId>io.netty</groupId><artifactId>netty-all</artifactId><version>4.1.97.Final</version></dependency>服务端一个接口IGet......
  • java PDF转换图片(多张pdf转换成一整张图片)
    引入pdf操作相关pom <dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.21</version></dependency>具体代码@RequestMappin......
  • Java枚举类
    一、使用场景:什么情况下使用枚举类?有的时候一个类的对象的个数是固定的,这种情况下我们就应该用枚举类来表示这个类。比如表示星期,就可以将Week定义为一个枚举类,同时为Week枚举类创建七个对象。再比如表示季节,就可以将Season定义为一个枚举类,同时为Season枚举类创建四个对象。......
  • 界面组件DevExpress WPF中文教程 - 如何从CRTX模板文件导入图表设置
    DevExpressWPF 拥有120+个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpressWPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。无论是Office办公软件的衍伸产品,还是以数据为中......
  • java01基础入门
    java01基础入门准备javac-versionjava-versioncd..//回到上一级勾选文件扩展名设置Path环境变量IDEA创建工程开发步骤project->module->package->class设置主题、字体快捷键注释关键字基本组成:由数字、字母、下划线(_)和美元符($)等组......
  • 一个Java基于codePoint的emoji判断方法
    该方法参考自一篇博客java判断是否是emoji字符(史上最全)_Mr.QingBin的博客-CSDN博客_java判断emoji经过简单封装如下:publicclassEmojiFilter{privateEmojiFilter(){}/***过滤emoji或者其他非文字类型的字符*如果只需要判断是否含有emoji,使用hasEmoji......