favorite We need a little bit of your help to keep things running, click on this banner to learn more

2013 Petrozavodsk, February 2

GOV-internship 3

Definition. Hamming distance between two strings of equal length is the number of positions in which these strings differ.

Definition. Distance from text s to pattern p is the sum of all Hamming distances between the pattern and all substrings of text which have length |p|.

You are given text s and pattern p. Either text or pattern, but not both, can be damaged (some symbols are lost). Your task is to reconstruct the damaged string so that the Hamming distance between the text and the pattern will be the least possible.


The first line contains the length n (1 n 100000) of the text s. The second line contains the text represented as n integer numbers ti (0 ti100000) separated by whitespaces. The third line contains one integer m: the length of the pattern p (1 m < n). The fourth line contains the pattern in the same format. Positive numbers denote the number of the symbol in the alphabet, while zero denotes a lost symbol. It is guaranteed that if there are lost symbols in text, there are none it pattern and vice versa.


Output the text and the pattern separated with a line break. All lost symbols must be reconstructed. If there are several ways to reconstruct symbols optimally, output any one of them.

Time limit 1 second
Memory limit 64 MiB
Input example #1
1 2 3 1 2
1 2 0
Output example #1
1 2 3 1 2
1 2 3
Author M.Rubinchik, B.Zaynullin
Source 2013 Petrozavodsk, Winter, Ural FU contest, Kontur Cup, Problem I