Pagini recente » Cod sursa (job #1575146) | Cod sursa (job #1298117) | Cod sursa (job #1941843) | Cod sursa (job #1054831) | Cod sursa (job #2042148)
#include <fstream>
#include <cstring>
#include <iostream>
using namespace std;
ifstream f ("strmatch.in");
ofstream g ("strmatch.out");
const int nmax=2e6+3;
int n,m,usu;
char a[nmax],b[nmax];
int pi[nmax],pos[1024];
void make_prefix()
{
int q=0;
for(int i=2;i<=m;++i)
{
while(q&&a[q+1]!=a[i]) q=pi[q];
if(a[q+1]==a[i]) ++q;
pi[i]=q;
}
}
int main()
{
int q=0;
f.getline(a+1,nmax);
f.getline(b+1,nmax);
m=strlen(a+1);
n=strlen(b+1);
make_prefix();
for(int i=1;i<=n;++i)
{
while(q&&a[q+1]!=b[i]) q=pi[q];
if(a[q+1]==b[i]) ++q;
if(q==m)
{
q=pi[m];
++usu;
if(usu<=1000) pos[usu]=i-m;
}
}
g<<usu<<'\n';
for(int i=1;i<=min(usu,1000);++i) g<<pos[i]<<' ';
return 0;
}