Cod sursa(job #369343)

Utilizator worstbyteelev gigel worstbyte Data 28 noiembrie 2009 09:03:15
Problema Potrivirea sirurilor Scor 18
Compilator cpp Status done
Runda Arhiva educationala Marime 0.53 kb
#include<stdio.h>
#include<string.h>
#define NM 2000001

char M[NM],N[NM];
int m,n,U[NM],sol[1009],k;

void prefix(){
int i,j=-1;
U[0]=-1;
for(i=0;i<n;++i){
	while(j>-1 && N[i]!=M[j+1]) j=U[j];
	if(N[i]==M[j+1]) j++;
	if(j==m-1){
		k++;
		if(k<=1000) sol[k]=i-m+1;
		U[i]=j;
		}
	}
}

int main(){
freopen("strmatch.in","r",stdin);
freopen("strmatch.out","w",stdout);
scanf("%s%s",M,N);
m=strlen(M);
n=strlen(N);
prefix();
int i;
printf("%d\n",k);
for(i=1;i<=k&&i<=1000;++i) printf("%d ",sol[i]);
return 0;
}