Cod sursa(job #151853)

Utilizator a7893Nae Mihai a7893 Data 8 martie 2008 18:18:46
Problema Triplete Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include<stdio.h>
#include<string.h>
#define N 2000001
char p[N],t[N];
int n,m,urm[N],poz[1025],cnt;
void urmt(void)
{
	int k=-1,x;
	urm[0]=0;
	for(x=1;x<m;x++)
	{
		while(k>0&&p[k+1]!=p[x])
			k=urm[x];
		if(p[k+1]==p[x])
			k++;
		urm[x]=k;
	}
}
int min(int a,int b)
{
	return a<b?a:b;
}
int main()
{
	freopen("strmatch.in","r",stdin);
	freopen("strmatch.out","w",stdout);
	int i,x=-1;
	scanf("%s\n%s",&p,&t);
	//n=strlen(t);
	//m=strlen(p);
	for (; (p[m] >= 'A' && p[m] <= 'Z') || (p[m] >= 'a' && p[m] <= 'z') 
			|| (p[m] >= '0' && p[m] <= '9'); ++m);
	for (; (t[n] >= 'A' && t[n] <= 'Z') || (t[n] >= 'a' && t[n] <= 'z')
			|| (t[n] >= '0' && t[n] <= '9'); ++n);
	urmt();
	for(i=0;i<n;i++)
	{
		while(x>0&&p[x+1]!=t[i])
			x=urm[x];
		if(p[x+1]==t[i])
			x++;
		if(x==m-1)
		{
			cnt++;
			if(cnt<=1000)
				poz[cnt]=i-m+1;
			x=urm[x];
		}
	}
	//printf("%d\n",min(cnt,1000));
	printf("%d\n",cnt);
	for(i=1;i<=min(cnt,1000);i++)
		printf("%d ",poz[i]);
	printf("%d\n",poz[i]);
	return 0;
}