题库 软件开发 题目列表 问题描述: Please implement KMP(Knuth-Morris-Prat...
问答题
问题描述:
Please implement KMP(Knuth-Morris-Pratt) pattern search algorithm using Java. The implementation should:

Read haystack from a file.

Read pattern string from standard input

Print the LPS array like “[1,3,5,...], separated with “, ”(comma with a suffix space).

Print each position of pattern starting from position 0 for every comparison, until the first match is encountered searching ends with no match, one index number per line.

Print the starting index of the match if found as “Found at {n}”, {n} should be the index, e.g. "Found at 12". Print “Not found” if no match.
输入描述:
read 2 lines from stdin, line 1 is pattern, line 2 is haystack.
输出描述:
print LPS array;
print pattern position for each comparison;
print result, "Found at: N" if pattern found or "Not found" if no matches found.
输入样例:
albania alb1albanialbaalbaniarfgh
输出样例:
[0, 0, 0, 1, 0, 0, 1] 0
1
2
0
0
1
2
3
4
5
6
Found at: 4
题目信息
校招真题
-
正确率
0
评论
38
点击