KMP(字符串匹配)算法
unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls; type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
//模式数组
arrNext : array of Integer;
//主串数组
Schars : array of AnsiChar;
//字串数组
Dchars : array of AnsiChar; //获取模式数组
procedure GetNext;
//查找匹配
function KPM(sPos:Integer):integer;
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm} { TForm1 } procedure TForm1.GetNext;
var
len,m,n:Integer;
begin
m := ;
n := -;
len := Length(Dchars);
SetLength(arrNext,len);
arrNext[] := -;
while (m+)<= len do
begin
if (n = -) or (Dchars[m] = Dchars[n]) then
begin
Inc(m);
Inc(n);
if (Dchars[m] <> Dchars[n]) then
arrNext[m] := n
else
arrNext[m] := arrNext[n];
end
else
begin
n := arrNext[n];
end;
end;
end; function TForm1.KPM(sPos:Integer): integer;
var
i,j:Integer;
begin
Result := ;
i := sPos;
j := ;
while (i < Length(Schars)) and (j < Length(Dchars)) do
begin
if (Schars[i] = Dchars[j]) then
begin
Inc(i);
Inc(j);
end
else
begin
Result := Result + j - arrNext[j];
if arrNext[j] <> - then
begin
j := arrNext[j];
end
else
begin
j := ;
Inc(i);
end;
end;
end;
if j <> Length(Dchars) then
Result := -;
end; procedure TForm1.Button1Click(Sender: TObject);
var
s,d:string;
index : Integer;
begin
s := '中华人民共和国';
d := '人民';
index := -;
SetLength(Schars,Length(s));
SetLength(Dchars,Length(d));
Move(s[],Schars[],Length(s));
Move(d[],Dchars[],Length(d));
GetNext;
index := KPM();
if index = - then
ShowMessage('没有找到匹配!')
else
ShowMessage('从第'+IntToStr(index+)+'个字符开始匹配');
end; end.
KMP(字符串匹配)算法的更多相关文章
- Luogu 3375 【模板】KMP字符串匹配(KMP算法)
Luogu 3375 [模板]KMP字符串匹配(KMP算法) Description 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来 ...
- 洛谷P3375 [模板]KMP字符串匹配
To 洛谷.3375 KMP字符串匹配 题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.如果 ...
- P3375 【模板】KMP字符串匹配
P3375 [模板]KMP字符串匹配 https://www.luogu.org/problemnew/show/P3375 题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在 ...
- 洛谷—— P3375 【模板】KMP字符串匹配
P3375 [模板]KMP字符串匹配 题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next. (如 ...
- KMP字符串匹配 模板 洛谷 P3375
KMP字符串匹配 模板 洛谷 P3375 题意 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.(如果 ...
- {Reship}{KMP字符串匹配}
关于KMP字符串匹配的介绍和归纳,作者的思路非常清晰,推荐看一下 http://blog.csdn.net/v_july_v/article/details/7041827
- 洛谷P3375 - 【模板】KMP字符串匹配
原题链接 Description 模板题啦~ Code //[模板]KMP字符串匹配 #include <cstdio> #include <cstring> int cons ...
- P3375 模板 KMP字符串匹配
P3375 [模板]KMP字符串匹配 来一道模板题,直接上代码. #include <bits/stdc++.h> using namespace std; typedef long lo ...
- KMP字符串匹配学习
KMP字符串匹配学习 牛逼啊 SYC大佬的博客
随机推荐
- ES6 对象的扩展 Object.is()
ES5 比较两个值是否相等,只有两个运算符:相等运算符(==)和严格相等运算符(===).它们都有缺点,前者会自动转换数据类型,后者的NaN不等于自身,以及+0等于-0. ES6 提出“Same-va ...
- TEST mathjax
这里是第一个公式 $ F = ma^2 $ \[ \text{Reinforcement Learning} \doteq \pi_* \\ \quad \updownarrow \\ \pi_* \ ...
- 网络流Ek算法
例题: Flow Problem HDU - 3549 Edmonds_Karp算法其实是不断找增广路的过程. 但是在找的过程中是找"最近"的一天增广路, 而不是找最高效的一条增 ...
- [LeetCode&Python] Problem 401. Binary Watch
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...
- Dependency Parsing -13 chapter(Speech and Language Processing)
https://web.stanford.edu/~jurafsky/slp3/13.pdf constituent-based 基于成分的phrasal constituents and phras ...
- set_union和set_intersection
set_union使用方法: set_union(集合A起始地址,集合A终止地址,集合B起始地址,集合B终止地址,插入C集合中(操作:inserter(C.C.begin()))); set_inte ...
- windows 10 下sublime text 3配置c/c++编译环境
来源于在网上各种帖子,自己成功配置之后写作笔记: 检查环境变量 首先配置重定义环境变量,过程如下: 创建一个test.c文件 内容: #include <stdio.h> int main ...
- Mac下常用工具
MySql--Sequel Pro
- 2017.4.28 KVM 内存虚拟化及其实现
概述 KVM(Kernel Virtual Machine) , 作为开源的内核虚拟机,越来越受到 IBM,Redhat,HP,Intel 等各大公司的大力支持,基于 KVM 的开源虚拟化生态系统也日 ...
- hihocoder1489 Legendary Items 概率期望
Little Hi is playing a video game. Each time he accomplishes a quest in the game, Little Hi has a ch ...