kmp算法&&学习感想
2016-12-18 16:02
483 查看
kmp
package 串的模式匹配; /** * Created by h on 16-12-18. */ public class Solution { public int findAppearance(String A, int lena, String B, int lenb) { int[] next = new int[lenb]; buildNext(next, B, lenb); int i = 0; int j = 0; while (true) { if (i == lena && j != lenb) { break; } if (j == lenb) { return i - j; } if (j < 0 || A.charAt(i) == B.charAt(j)) { i++; j++; } else { j = next[j]; } } return -1; } private void buildNext(int[] next, String P, int lenP) { next[0] = -1; for (int i = 1; i < lenP; i++) { if (next[i-1] < 0 || P.charAt(i - 1) == P.charAt(next[i - 1])) { next[i] = next[i - 1] + 1; } else { next[i] = 0; } } // for (int i: // next) { // System.out.print(i + " "); // } // System.out.println(); } public static void main(String[] args) { String A = "djkfkdjfkldjfldj"; int lenA = A.length(); String B = "fk"; int lenB = B.length(); System.out.println(new Solution().findAppearance(A, lenA,B,lenB)); } }
学习感想
这个算法我在这两年里学习了四遍,现在可以快速地独立地手写。说明有时候要吃透一个知识点,要反反复复学习好几遍 ,很多以前会的东西,现在回头去看,居然可以有更深刻的理解。
相关文章推荐
- 寒假数据结构与算法学习&amp;寒假训练赛与新春大作战总结感想
- .NET系统学习----Globalization & Resources
- <kingofark关于学习C++和编程的50个观点> 详解 - 预览版
- 学习Biztalk 2004的感想
- Linux学习笔记之后台进程(at和&)
- 我的css学习感想!
- Managed DirectX® 9 学习笔记(1)
- OOAD&amp;UML学习笔记
- 2005年1月8日,学习感想
- 学习java&xml心得(二)
- 《自适应软件开发》—从奴隶主到领袖< 5 预测—协作—学习 >
- C++的学习感想(一)
- "HOW TO: Create a Smooth Progress Bar in Visual C# .NET"的学习笔记
- 执行数据库命令(Command对象)——ADO.NET学习&应用笔记之三
- C++的学习感想(二)
- 泛型(Generics Types)学习笔记<一>
- Generics Types 泛型学习笔记<三>
- .NET系统学习----Globalization & Resources
- C++的学习感想
- Managed DirectX® 9学习笔记 ycl