#1015 : KMP算法
来源:互联网 发布:火车票抢购软件 编辑:程序博客网 时间:2024/05/29 07:01
kmp用得不熟练!!!
import java.util.Scanner;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Main {public static int max = 10010;public static int[] next = new int[max];public static void main(String[] args) {// TODO Auto-generated method stubScanner scanner = new Scanner(System.in);int N = scanner.nextInt();scanner.nextLine();String pattern;String lineString;for (int i = 0; i < N; i++) {pattern = scanner.nextLine();lineString = scanner.nextLine();getNext(pattern);int count = getMatchCount(pattern, lineString);System.out.println(count);}}private static int getMatchCount(String pattern, String lineString) {// TODO Auto-generated method stubint i = 0, j = 0, cnt = 0;int count = 0;if (lineString.length() < pattern.length()) {return 0;}while (i < lineString.length()) {if <span style="color:#ff0000;">(j == -1 </span>|| lineString.charAt(i) == pattern.charAt(j)) {i++;j++;}else {j = next[j];}if (j == pattern.length()) {count++;<span style="color:#ff0000;">j = next[j];</span>}}return count;}private static void getNext(String pattern){int pLen =pattern.length();next[0] = -1;int k = -1;int j = 0;while (j < pLen) {if (k == -1 || pattern.charAt(j) == pattern.charAt(k)) {k++;j++;next[j] = k;}else {k = next[k];}}}}
0 0
- #1015 : KMP算法 裸kmp
- ACM #1015 KMP算法
- hihoCoder - 1015 - KMP算法
- [HiHoCoder]#1015 : KMP算法
- hihoCoder#1015 KMP算法
- hihocoder#1015 : KMP算法
- #1015 : KMP算法
- hihoCoder 1015 : KMP算法
- hihoCoder #1015 : KMP算法
- hihoCoder#1015 KMP算法
- #1015 : KMP算法
- hihocoder #1015 KMP算法
- 1015 : KMP算法
- [Hiho]1015-KMP算法
- hihoCoder 1015 KMP算法
- hihoCoder 1015 KMP算法
- #1015 : KMP算法
- hihocoder #1015 kmp算法
- Mongodb数据 导入导出数据 导出工具mongoexport和导入工具mongoimport介绍
- IO:同步、异步、阻塞、非阻塞的区别
- Java高性能集合类 ConcurrentLinkedHashMap demo
- Minimum Depth of Binary Tree
- 黑马程序员_java12_java中的异同
- #1015 : KMP算法
- Android判断当前系统语言
- jquery的jquery c.browser msie undefined的问题解决办法
- 【论坛】网络与通信大板上线公告
- 老式后门之美:五种复古远程控制工具(含下载)
- android studio给项目添加第三方库的方法
- MongoDB删除字段和部分字段查询
- Android Fragment生命周期
- U8SDK问答及汇总