KMP算法java实现

    xiaoxiao2022-08-06  9


    某平台价值19860元的编程课程资料免费领取【点我领取】


    package MyPackage; import java.util.Arrays; import java.util.Scanner; import java.io.*; import java.lang.management.MemoryType; import java.util.*; public class KMP { public static void main(String args[]) { String string="Asdgew"; String aString="wrsdfagaAsdgew sdgewsdf Asdgew"; int[] res=gererateShuZu(string); int end=0; for(int i=0;i<aString.length();i++){ while(end>0&&aString.charAt(i)!=string.charAt(end)){ end=res[end-1]; }; if(aString.charAt(i)==string.charAt(end))end++; if(end==string.length()){ System.out.println(i-string.length()+1+" "); end=res[end-1]; } } } static int[] gererateShuZu(String str){ int end=0; int[] res=new int[str.length()]; for(int i=1;i<str.length();i++){ while(end>0&&str.charAt(i)!=str.charAt(res[end])){ end=res[end-1]; } if(str.charAt(i)==str.charAt(res[end])){ end++; } res[i]=end; } return res; } }
    转载请注明原文地址: https://ju.6miu.com/read-1132068.html
    最新回复(0)