Cod sursa(job #1501785)

Utilizator ili226Vlad Ilie ili226 Data 13 octombrie 2015 20:40:36
Problema Potrivirea sirurilor Scor 14
Compilator fpc Status done
Runda Arhiva educationala Marime 0.97 kb
{$R-}
var a,b:array[1..2000003]of char;
    pi:array[1..2000003]of longint;
    r:array[1..1000]of longint;
    f,fo:text;
    n,m,i,j,k,l:longint;
begin
assign(f,'strmatch.in');
assign(fo,'strmatch.out');
reset(f);
n:=0;m:=0;
while not(eoln(f)) do
 begin inc(n);
       read(f,a[n]);
 end;
readln(f);
while not(eoln(f)) do
 begin inc(m);
       read(f,b[m]);
 end;
close(f);
k:=0;
pi[1]:=k;
for i:=2 to n do
 begin
  while (k>0)and(a[i]<>a[k+1])do
   k:=pi[k];
  if a[i]=a[k+1] then
   inc(k);
  pi[i]:=k;
 end;
k:=0;j:=0;
for i:=1 to m do
 begin
  while (j>0)and(a[j+1]<>b[i])do
   j:=pi[j];
  if a[j+1]=b[i] then
   inc(j);
  if j=n then
   begin
    inc(k);
    r[k]:=i-n+1;
    if k=1000 then
     begin
      rewrite(fo);
      writeln(fo,k);
      for l:=1 to k do
       write(fo,r[l],' ');
      close(fo);
      halt
     end;
   end
 end;
rewrite(fo);
writeln(fo,k);
for i:=1 to k do
 write(fo,r[i],' ');
close(fo);
end.