412C.md (2418B)
1 Source: https://codeforces.com/problemset/problem/412/C 2 3 C. Pattern 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 stdin 16 17 output 18 19 stdout 20 21 Developers often face with regular expression patterns. A pattern is usually defined as a string consisting of characters and metacharacters that sets the rules for your search. These patterns are most often used to check whether a particular string meets the certain rules. 22 23 In this task, a pattern will be a string consisting of small English letters and question marks ('?'). The question mark in the pattern is a metacharacter that denotes an arbitrary small letter of the English alphabet. We will assume that a string matches the pattern if we can transform the string into the pattern by replacing the question marks by the appropriate characters. For example, string aba matches patterns: ???, ??a, a?a, aba. 24 25 Programmers that work for the R1 company love puzzling each other (and themselves) with riddles. One of them is as follows: you are given *n* patterns of the same length, you need to find a pattern that contains as few question marks as possible, and intersects with each of the given patterns. Two patterns intersect if there is a string that matches both the first and the second pattern. Can you solve this riddle? 26 27 Input 28 29 The first line contains a single integer *n* (1 ≤ *n* ≤ 105) — the number of patterns. Next *n* lines contain the patterns. 30 31 It is guaranteed that the patterns can only consist of small English letters and symbols '?'. All patterns are non-empty and have the same length. The total length of all the patterns does not exceed 105 characters. 32 33 Output 34 35 In a single line print the answer to the problem — the pattern with the minimal number of signs '?', which intersects with each of the given ones. If there are several answers, print any of them. 36 37 Examples 38 39 Input 40 41 ``` 42 2 43 ?ab 44 ??b 45 ``` 46 47 Output 48 49 ``` 50 xab 51 ``` 52 53 Input 54 55 ``` 56 2 57 a 58 b 59 ``` 60 61 Output 62 63 ``` 64 ? 65 ``` 66 67 Input 68 69 ``` 70 1 71 ?a?b 72 ``` 73 74 Output 75 76 ``` 77 cacb 78 ``` 79 80 Note 81 82 Consider the first example. Pattern xab intersects with each of the given patterns. Pattern ??? also intersects with each of the given patterns, but it contains more question signs, hence it is not an optimal answer. Clearly, xab is the optimal answer, because it doesn't contain any question sign. There are a lot of other optimal answers, for example: aab, bab, cab, dab and so on.