首页 > 学院 > 开发设计 > 正文

hihocoder#1015:(KMP)

2019-11-08 03:06:53
字体:
来源:转载
供稿:网友
题目: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;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表