Hiho_1015:KMP算法
来源:互联网 发布:od矩阵模型 编辑:程序博客网 时间:2024/06/06 03:04
1015:KMP 算法
题目解答:
import java.util.Arrays;import java.util.Scanner;public class Main{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); for (int i = 0; i < n; i++) { char[] p = scanner.next().toCharArray(); char[] s = scanner.next().toCharArray(); int[] next = new int[10005]; Arrays.fill(next, -1); getNextVal(p, next); int count = kmpSearch(s, p, next); System.out.println(count); } } public static int kmpSearch(char[] s, char[] p, int[] next) { int i = 0, j = 0; int sLen = s.length; int plen = p.length; int count = 0; while (i < sLen) { if (j == -1 || s[i] == p[j]) { i++; j++; } else { j = next[j]; } if (j == plen) { count++; j = next[j]; } } return count; } public static void getNextVal(char[] p, int[] next) { int pLen = p.length; next[0] = -1; int k = -1; int j = 0; while (j < pLen) { if (k == -1 || p[k] == p[j]) { k++; j++; next[j] = k; } else { k = next[k]; } } }}
阅读全文
0 0
- Hiho_1015:KMP算法
- KMP算法详解 【KMP】
- 【KMP】KMP算法模板
- KMP hihoCoder1015 KMP算法
- kmp算法
- KMP算法
- KMP算法
- KMP算法
- KMP算法
- KMP 算法
- kmp算法
- KMP算法
- kmp算法
- KMP算法
- KMP算法
- kmp算法
- kmp算法
- KMP算法
- 1118: 数列有序
- 为什么有这么多 Python?Python是解释型的还是编译型的?
- OpenStack云环境数据备份方案 Freezer
- poj 2456 Aggressive cows
- 计算机网络自顶向下方法第二章笔记
- Hiho_1015:KMP算法
- epel
- 5.2
- 一张细化了的hbase存储结构图
- (一)数据结构与算法--前篇
- STL之list容器详解
- 价值200元的小程序卡卷开发免费送
- 图像相关(三) Bitmap与byte[]、BitmapImage与byte[]互相转换、图像加载与保存
- centos7基于lnmp配置wordpress