Pagini recente » Profil teo1496 | Istoria paginii utilizator/prunacosmin1 | Cod sursa (job #460964) | Monitorul de evaluare | Cod sursa (job #1988064)
#include <iostream>
#include <fstream>
#define Nmax 200005
using namespace std;
ifstream fin("strmatch.in");
ofstream fout("strmatch.out");
string A,B;
int N,M,pi[Nmax],n,poz[Nmax];
void make_prefix()
{
int q=0;
pi[1]=0;
for(int i=2;i<=N;i++)
{
while(q && A[q+1]!=A[i])q=pi[q];
if(A[q+1]==A[i])q++;
pi[i]=q;
}
}
int main()
{
fin>>A>>B;
N=A.size();
M=B.size();
for (int i = N; i; --i) A[i] = A[i-1]; A[0] = ' ';
for (int i = M; i; --i) B[i] = B[i-1]; B[0] = ' ';
make_prefix();
int q=0;
for(int i=1;i<=M;i++)
{
while(q && A[q+1]!=B[i])q=pi[q];
if(A[q+1]==B[i])q++;
if(q==N)
{
q=pi[N];
n++;
if(n<=1000)poz[n]=i-N;
}
}
fout<<n<<'\n';
for(int i=1;i<=n;i++)fout<<poz[i]<<" ";
return 0;
}