Pagini recente » Cod sursa (job #3194484) | Cod sursa (job #2854556) | Cod sursa (job #864041) | Cod sursa (job #2488950) | Cod sursa (job #989857)
Cod sursa(job #989857)
#include<fstream>
#include<cstring>
using namespace std;
ifstream f("strmatch.in");
ofstream g("strmatch.out");
int n,m,k,nr,p[2000005],poz[1005];
char A[2000005],B[2000005];
void prefix()
{
int i,q=0;
p[1]=0;
for (i=2;i<=m;++i)
{
while (q>0 && A[q+1]!=A[i])
q=p[q];
if (A[q+1]==A[i]) ++q;
p[i]=q;
}
}
int main()
{
int i;
f.getline(A+1,2000004);
f.getline(B+1,2000004);
A[0]=B[0]=' ';
n=strlen(B)-1;
m=strlen(A)-1;
prefix();
for (i=1;i<=n;++i)
{
while (k>0 && A[k+1]!=B[i])
k=p[k];
if (A[k+1]==B[i]) ++k;
if (k==m)
{
if (nr<1000)
poz[++nr]=i-m;
else ++nr;
k=p[k];
}
}
g<<nr<<'\n';
for (i=1;i<=min(nr,1000);++i)
g<<poz[i]<<' ';
g<<'\n';
return 0;
}