Pagini recente » Cod sursa (job #2161068) | Cod sursa (job #539185) | Cod sursa (job #768511) | Cod sursa (job #1813827) | Cod sursa (job #1075377)
#include<fstream>
#include<algorithm>
#include<string.h>
#define Nmax 2000099
using namespace std;
ifstream f("strmatch.in");
ofstream g("strmatch.out");
char A[Nmax],B[Nmax];
int N,M,pi[Nmax],q,aparitii,i,Sol[1005];
int main()
{
f>>A+1>>B+1;
N=strlen(A+1); M=strlen(B+1);
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;
}
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 && ++aparitii<=1000) Sol[aparitii]=i-N;
}
g<<aparitii<<'\n';
for(int i=1;i<=min(aparitii,1000);++i)g<<Sol[i]<<' ';
f.close();g.close();
return 0;
}