论熟练掌握KMP的重要性

var
  a,b:array[1..100000] of char;
  next:array[0..100000] of longint;
  t:boolean;
  len1,len2,i,x:longint;
  ch:char;
begin
  read(ch);len1:=0;
  while ord(ch)<>13 do begin
    inc(len1);a[len1]:=ch;read(ch);
  end;readln;
  read(ch);len2:=0;
  while ord(ch)<>13 do begin
    inc(len2);b[len2]:=ch;read(ch);
  end;readln;
  next[0]:=0;next[1]:=0;
  for i:=2 to len2 do begin
    x:=next[i-1];
    while (x>0) and (b[x+1]<>b[i]) do x:=next[x];
    if b[x+1]=b[i] then next[i]:=x+1 else next[i]:=0;
  end;
  t:=false;x:=0;
  for i:=1 to len1 do begin
    while (x>0) and (b[x+1]<>a[i]) do x:=next[x];
    if b[x+1]=a[i] then inc(x);
    if x=len2 then begin
      t:=true;break;
    end;
  end;
  writeln(t);
end.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值