Pagini recente » Cod sursa (job #124467) | Cod sursa (job #787776) | Cod sursa (job #1981825) | Cod sursa (job #273006) | Cod sursa (job #151740)
Cod sursa(job #151740)
#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",cnt);
for(i=1;i<min(cnt,1000);i++)
printf("%d\n",poz[i]);
return 0;
}