KMP String Matching Algorithm

  This is a program based on Knuth-Morris-Pratt String Matching Algorithm (a.k.a KMP algorithm), which can solve the problem POJ 3461.

 1 import java.io.*;
 2 import java.util.*;
 3
 4 class Input {
 5     private Scanner in;
 6     private StringTokenizer tok;
 7
 8     public Input() {
 9         in = new Scanner(new BufferedInputStream(System.in));
10     }
11     public String nextString() {
12         while  (tok==null||!tok.hasMoreTokens()) {
13             tok = new StringTokenizer(in.nextLine());
14         }
15         return tok.nextToken();
16     }
17     public int nextInt() {
18         while  (tok==null||!tok.hasMoreTokens()) {
19             tok = new StringTokenizer(in.nextLine());
20         }
21         return Integer.parseInt(tok.nextToken());
22     }
23     public double nextDouble() {
24         while  (tok==null||!tok.hasMoreTokens()) {
25             tok = new StringTokenizer(in.nextLine());
26         }
27         return Double.parseDouble(tok.nextToken());
28     }
29     public void close() {
30         in.close();
31     }
32 }
33
34 public class Main {
35
36     public static int[] preproc(String pattern)  {
37         int[] dp = new int[pattern.length()];
38         Arrays.fill(dp,-1);
39         for (int i=1;i<pattern.length();i++) {
40             for (int j=i-1;j>=0;j=dp[j]) {
41                 if (pattern.charAt(dp[j]+1)==pattern.charAt(i)) {
42                     dp[i] = dp[j]+1;
43                     break;
44                 }
45             }
46         }
47         return dp;
48     }
49     public static int match(String pattern,String test) {
50         int[] next = preproc(pattern);
51         int cnt = 0;
52         for (int i=0,j=0;i<test.length();) {
53             if (test.charAt(i)==pattern.charAt(j)) {
54                 ++i;
55                 if (++j==pattern.length()) {
56                     cnt++;
57                     j = next[j-1]+1;
58                 }
59             } else if (j>0) {
60                 j = next[j-1]+1;
61             } else {
62                 i++;
63             }
64         }
65         return cnt;
66     }
67     public static void main(String[] args) {
68         Input in = new Input();
69         int time = in.nextInt();
70         for (int t=0;t<time;t++) {
71             System.out.println(match(in.nextString(),in.nextString()));
72         }
73         in.close();
74     }
75 }
时间: 2024-07-28 20:42:04

KMP String Matching Algorithm的相关文章

String Matching -- Brute Force + Rabin-Karp + KMP

String Matching 这个问题已经被做烂了... 下面是C语言实现集合. http://www-igm.univ-mlv.fr/~lecroq/string/ 留个爪- 暴力解法: 暴力美啊- """ Programmer : EOF Date : 2015.02.28 Code file : nsm.py """ def naive_string_matcher(T, P) : if (T or P) is None : return

NYOJ 5 Binary String Matching (kmp 字符串匹配)

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only '0' and '1'. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is '100111011

nyoj5 Binary String Matching(KMP)

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only '0' and '1'. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is '100111011

[POJ] String Matching

String Matching Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4074   Accepted: 2077 Description It's easy to tell if two words are identical - just check the letters. But how do you tell if two words are almost identical? And how close

HDU1306 String Matching 【暴力】

String Matching Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 847    Accepted Submission(s): 434 Problem Description It's easy to tell if two words are identical - just check the letters. But

九度OJ 1094 String Matching

题目1094:String Matching 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1098 解决:587 题目描述: Finding all occurrences of a pattern in a text is a problem that arises frequently in text-editing programs. Typically,the text is a document being edited,and the pattern searched

NYOJ 5 Binary String Matching【string find的运用】

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only '0' and '1'. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is '100111011

NYOJ5 Binary String Matching

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only ‘0’ and ‘1’. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is ‘100111011

NYOJ题目5---Binary String Matching

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only '0' and '1'. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is '100111011