hihocoder#1015:(KMP)

题目:http://hihocoder.com/problemset/problem/1015
题目分析:KMP裸题,GDKOI前敲敲模板……
CODE:
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<stdio.h>
#include<algorithm>
using namespace std;

const int maxl=10010;

int Next[maxl];

string s,t;
int n;

int main()
{
	freopen("c.in","r",stdin);
	freopen("c.out","w",stdout);
	
	scanf("%d",&n);
	for (int i=1; i<=n; i++)
	{
		cin>>t;
		cin>>s;
		int tlen=t.size();
		int slen=s.size();
		
		Next[0]=Next[1]=0;
		int k=0;
		for (int i=2; i<=tlen; i++)
		{
			while ( k && t[k]!=t[i-1] ) k=Next[k];
			if ( t[k]==t[i-1] ) k++;
			Next[i]=k;
		}
		
		k=0;
		int ans=0;
		for (int i=1; i<=slen; i++)
		{
			while ( k && t[k]!=s[i-1] ) k=Next[k];
			if ( t[k]==s[i-1] ) k++;
			if ( k==tlen ) ans++;
		}
		
		printf("%d\n",ans);
	}
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值