Pagini recente » Cod sursa (job #299174) | Cod sursa (job #3151570) | Cod sursa (job #1807249) | Cod sursa (job #1257485) | Cod sursa (job #2198250)
#include <fstream>
#include <cstring>
using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
const int nmax=2e6+3;
int n,m,c,pi[nmax],pos[1024];
char a[nmax],b[nmax];
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;
fin.getline(a+1,nmax);
fin.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];
++c;
if(c<=1000) pos[c]=i-m;
}
}
fout<<c<<endl;
for(int i=1; i<=min(c,1000); ++i) fout<<pos[i]<<' ';
return 0;
}