Pagini recente » Cod sursa (job #1294999) | Cod sursa (job #2585709) | Cod sursa (job #1833534) | Cod sursa (job #477010) | Cod sursa (job #561147)
Cod sursa(job #561147)
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char t[2000002],p[2000002];
int lT,lP,l[2000002],vtime=0,times[1002];
void createL()
{
int k;
l[1]=0;
for(int i=2;i<=strlen(p+1)-1;i++)
{
k=l[i-1];
while(k>0&&p[k+1]!=p[i]) k=l[k];
if(p[k+1]==p[i]) k++;
l[i]=k;
}
}
void KMP()
{
int k=0;
for(int i=1;i<=strlen(t+1)-1;i++)
{
while(k>0&&p[k+1]!=t[i]) k=l[k];
if(p[k+1]==t[i])k++;
if(k==strlen(p+1)-1)
{
vtime++;
if (vtime<=1000)times[vtime]=i-(strlen(p+1)-1);
k=l[k];
}
}
}
int main()
{
freopen("strmatch.in","r",stdin);
freopen("strmatch.out","w",stdout);
/*gets(p2);
gets(t2);
strcat(t,t2);
strcat(p,p2);*/
fgets(p+1,2000002,stdin);
fgets(t+1,2000002,stdin);
createL();
KMP();
printf("%d",vtime);
printf("\n");
for(int i=1;i<=min(1000,vtime);i++) printf("%d ",times[i]);
//printf("%d\n",times[time]);
//printf("\n");
return 0;
}