Oulipo HDU - 1686

#include<iostream>
#include<cstring>
using namespace std ;
const int N=1e6+10;
int n,m;
int ne[N];
char p[N],s[N];
int main() {
	int t;
	cin>>t; 
	while(t--) {
		cin>>p+1>>s+1;
		n=strlen(p+1);
		m=strlen(s+1);
		int ans=0;
		for(int i=2,j=0; i<=n; i++) {
			while(j&&p[i]!=p[j+1]) j=ne[j];
			if(p[i]==p[j+1]) j++;
			ne[i]=j;
		}

		for(int i=1,j=0; i<=m; i++) {
			while(j&&s[i]!=p[j+1]) j=ne[j];
			if(s[i]==p[j+1]) j++;
			if(j==n) {
				ans++;
				j=ne[j];
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}
posted @ 2020-02-29 18:10  晴屿  阅读(81)  评论(0编辑  收藏  举报