A broken keyboard

3.4

22 votes
Algorithms, Basics of Greedy Algorithms, C++, Greedy Algorithms
Problem

Note: This is an approximate problem. There is no exact solution. You must find the most optimal solution. Refer to the sample explanation to ensure that you understand the problem correctly.

Problem statement

You are given N pattern strings. Find a string S that matches all the pattern strings.

A pattern string consists of lowercase Latin alphabets and * symbols. Each * may be replaced by any number of lowercase Latin characters. It is allowed to replace * with nothing, that is, remove the occurrence of * and concatenate the remaining strings.

A string S matches with a pattern string P, if there exists a way to achieve S from P by replacing * present in P.

Your task is to minimize the length of string S. (|S|  106)

Z = |S| is defined as the score of the test case.

Input format

  • The first line contains an integer N.
  • Next N lines contain a string P denoting a pattern string.

Output format

Print a string that matches all the patterns.

Note: |S| should be less than equal to 106

Verdict and scoring 

  • Your score is equal to the sum of the values of all test files. The value of each test file is equal to the value of Z.
  • If S does not matches with all the pattern strings or |S|  106, you will receive a Wrong Answer verdict.

Constraints

N=100

500|P|1000

A solution to every test case always exist.

There is at least one '*' character in each pattern string.

Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

String S = " raertaer" , can be generated from all the pattern strings. 

Hence, Z = |S| = 8.

Note : Sample Test Case is for understanding purpose. Test Cases strictly follow the constraints provided.

Editor Image

?