[ create a new paste ] login | about

Link: http://codepad.org/7ruXW33n    [ raw code | output | fork ]

didyxdi - C++, pasted on Sep 3:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<utility>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<cstring>
#include<cctype>
using namespace std;

int T;
char w[1000005],t[1000005];
int suffix[1000005];
int pw,pt,ans;
int wlen,tlen;

void init()
{
	memset(w,0,sizeof(w));
	memset(t,0,sizeof(t));
	memset(suffix,0,sizeof(suffix));
	ans=0;
}

int match()
{
	pw=pt=0;
	wlen=strlen(w);tlen=strlen(t);
	while(pt<tlen)
	{
		if(w[pw]==t[pt]) pw++,pt++;
		else if(pw>=0) pw=suffix[pw];
		else pw=0,pt++;
		
		if(pw==wlen)
		{
			ans++;
			pw=suffix[pw];
		}
	}
	return ans;//***
}

int main()
{
	while(scanf("%d",&T)!=EOF)
	{
		while(T--)
		{
			init();
			scanf("%s%s",w,t);
			
			int k=0;
			suffix[0]=-1;suffix[1]=0;
			for(pw=2;pw<=strlen(w);pw++)
			{
			    while(k>=0 && w[k]!=w[pw-1]) k=suffix[k];
				suffix[pw]=++k;
			}
			printf("%d\n",match());
	    }
	}
	return 0;
}


Output:
1
2
3
cc1plus: warnings being treated as errors
In function 'int main()':
Line 60: warning: comparison between signed and unsigned integer expressions


Create a new paste based on this one


Comments: