POJ3461 Oulipo ——KMP算法——Pku3461

建议大家学一学比较巧妙的KMP算法吧,很有意思。推荐个题目:POJ3167 Cow Patterns 题解我会发在本博里。

这个KMP就木有什么好说的了吧,大家找百度百科学一下就可以了~

CODE

Program KMP;//By_Thispoet
Const 
	maxn=1000005;
Var
	st,s						:Ansistring;//st is long and s is short
	pre							:Array[-1..maxn]of Longint;
	i,k,p,q,ans,n				:Longint;
	
BEGIN

	readln(n);
	while n>0 do 
		begin
			readln(st);
			readln(s);
			pre[1]:=0;
			for i:=2 to length(st) do 
				begin
					k:=pre[i-1];
					while (k<>0)and(st[k+1]<>st[i]) do k:=pre[k];
					if st[k+1]<>st[i] then pre[i]:=0 else pre[i]:=k+1;
				end;
			
			p:=0;q:=0;ans:=0;
			while q<length(s) do 
				begin
					inc(q);
					while (p<>0)and(st[p+1]<>s[q]) do p:=pre[p];
					if st[p+1]=s[q] then inc(p);
					if p=length(st) then 
						begin
							inc(ans);
							p:=pre[p];
						end;
				end;
			writeln(ans);
			dec(n);
		end;
	
END.

转载于:https://www.cnblogs.com/Thispoet/archive/2011/09/14/2175350.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值