KMP算法
来源:互联网 发布:java生成支付宝二维码 编辑:程序博客网 时间:2024/06/05 19:44
KMP算法
public class KmpmMethod {/** * 具体的匹配 * @param str1 * @param str2 * @return */ public static int kMPMatcher(String str1,String str2){ int i=0,j=-1; int arr[]=next(str2); while(i<str1.length()&&j<str2.length()){ if(j==-1||str1.charAt(i)==str2.charAt(j)){ i++; j++; } else j=arr[j]; } if(j==str2.length()) return i-j; return -1; } /** * next函数 * @param str * @return */ public static int[] next(String str){ int j=-1,i=0; int arr[]=new int[str.length()+1]; arr[0]=-1; while(i<str.length()){ if(j==-1||str.charAt(i)==str.charAt(j)){ j++; i++; arr[i]=j; } else j=arr[j]; } return arr; } public static void main(String[] args) { String str1="abcacbaaacabeabccaaa"; String str2="aacab"; int result=kMPMatcher(str1,str2); if(-1==result){ System.out.println("没有找到匹配的字串"); }else{ System.out.println("匹配的位置为"+result); }}}
0 0
- KMP算法详解 【KMP】
- 【KMP】KMP算法模板
- KMP hihoCoder1015 KMP算法
- kmp算法
- KMP算法
- KMP算法
- KMP算法
- KMP算法
- KMP 算法
- kmp算法
- KMP算法
- kmp算法
- KMP算法
- KMP算法
- kmp算法
- kmp算法
- KMP算法
- KMP算法
- 欢迎使用CSDN-markdown编辑器
- 内核3.x版本之后设备树机制
- 社説 20150820 知的財産の活用 法改正を競争力向上に生かせ
- HDOJ--2066--一个人的旅行
- tableHeaderView内使用自动布局总结
- KMP算法
- 重定向给网页的优化
- webservice
- jquery下载所有版本(实时更新)
- Linux进程的睡眠和唤醒(一个定时信号唤醒睡眠中的进程)
- ftp上传文件出现“sftp: cannot open /usr/SmartStore_test/1.zip to write”
- 社説 20150820 安保法案審議 成立後に向けた検討は当然だ
- Out of Hay
- iOS动态类型和动态绑定