
program SlaptasPranesimas ;

 const MAX_KEY_LEN = 60 ;
       MAX_PATTERN_LEN = 120 ;
       MAX_MSG_LEN = 20000 ;

 type PATTERN_T = array [1..MAX_PATTERN_LEN] of integer ;
      KEY_T = array [1..MAX_KEY_LEN] of integer ;

 var keyMin : KEY_T ;
     keyMinL : integer ;
     patternPos : integer ;
     pattern : PATTERN_T ;
     patternL : integer ;
     patternKey : PATTERN_T ;
     message : array [1..MAX_MSG_LEN] of integer ;
     msgL : integer ;

     ok : boolean ;
     i, j : integer ;
     keyPosInPattern : integer ;
     keyL : integer ;

     fIn, fOut : text ;

     positionsFound : integer ;
     fLog :text ;


 function verifyPatternKey(
                            var patternKey : PATTERN_T ;
                            patternL : integer ;
                            var keyL : integer
                          ) : boolean ;
   var i,j : integer ;
       ok : boolean ;
 begin

   ok := false ;

   keyL := 1 ;

   while not ok and ( keyL <= patternL div 2 ) do
   begin
{   writeln( '    Verify key len ', keyL ) ;}

    ok := true ;

    i := keyL + 1 ;
    while  ok and (i <= patternL) do
    begin
{      writeln( '    Verify pos', i ) ;}

      j := 1 ;
      while ok and (j <= keyL) and (i <= patternL) do
      begin
        ok := patternKey[j] = patternKey[i] ;
        j := j + 1 ;
        i := i + 1 ;
      end ;
    end ;

    if not ok then keyL := keyL + 1 ;

   end ;

   verifyPatternKey := ok ;
 end ;



begin

   assign( fIn, 'SLAPTAS.DAT' ) ;
   assign( fOut, 'SLAPTAS.REZ' ) ;

   reset( fIn ) ;
   readln( fIn, patternL ) ;
   for i := 1 to patternL do
     read( fIn, pattern[i] ) ;
   readln( fIn ) ;
   readln( fIn, msgL ) ;
   for i := 1 to msgL do
     read( fIn, message[i] ) ;
   close( fIn ) ;

positionsFound := 0 ;

   keyMinL := 0 ;
   i := 1 ;
   while ( i <= msgL - patternL + 1 ) do
    begin

     for j := 1 to patternL do
       patternKey[j] := (message[ i + j -1 ] + pattern[j]) mod 2 ;

     ok := verifyPatternKey( patternKey, patternL, keyL ) ;

     if ok then
     begin
      if (keyMinL = 0) or (keyMinL > keyL ) then
       begin
         patternPos := i ;
         keyMinL := keyL ;
         keyPosInPattern := 1 + keyL - (patternPos - 1) mod keyL ;
         for j := 1 to keyL do
           keyMin[j] := patternKey[keyPosInPattern + j - 1] ;


         positionsFound := 1 ;
       end
      else  {>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>}
        if (keyMinL > 0) and (keyMinL = keyL) then
           positionsFound := positionsFound + 1 ;

     end ;

     i := i + 1 ;
    end ;

   assign( fLog, 'SLAPTAS.LOG' ) ;
   rewrite(fLog) ;
   writeln( fLog, 'Positions found ', positionsFound ) ;
   close(fLog) ;

   rewrite( fOut ) ;

   if ( keyMinL = 0 ) then
   begin
     writeln( fOut, 0 ) ;
   end
   else
   begin

     writeln( fOut, keyMinL ) ;
     for j := 1 to keyMinL do
     begin
       write( fOut, keyMin[j]:1 ) ;
       if j < keyMinL then write( fOut, ' ' ) ;
     end ;
     writeln( fOut ) ;

     writeln( fOut, patternPos ) ;

     writeln( fOut, msgL ) ;
     i := 1 ;
     while i <= msgL do
     begin
       j := 1 ;
       while ( j <= keyMinL ) and ( i <= msgL ) do
       begin
         write( fOut, ((keyMin[j] + message[i]) mod 2):1 ) ;
         if i < msgL then write( fOut, ' ' ) ;

         j:= j + 1 ;
         i := i + 1 ;
       end ;
     end ;
     writeln( fOut ) ;

   end ;

   close( fOut ) ;

end.


