A company wants to send an important information to its clients, but they also want to keep it a secret. So they send two messages to clients, the first message contains the real_information, but mixed with fake information. The second_message contains only the real_information that is in first_message, but few characters in second_message are encrypted with special characters # and .Thesecondmessagecontainsonlyoneorzero.
The company also provided below instructions to the clients:
Assistant
The real information is always at the beginning of the first message
The real information in second_message is encypted in such a way that # represents any letter and representsthezeroormorerepetitionsofpreviousletterof in second_message
Now, the clients need to match the characters in second_message with the first message and extract the real information contained in first_message
For example, if the first message is assignment and second_message is asS#gn, $ and # in second message matches with s and i respectively. So, the real_information is assign.
Your task is to help clients find the real_information that is sent by the company.
Given first message and second_message, write a program to print the real_information by matching the characters in second_message with the first message by following the instructions. If there are multiple possible values of real_information, then print the one with the largest length.
Note
If the previous character of in second_message is #, matche any zero or more letters in first message
Example: If first message is combination and second_message is co#t # matches with m and matches with bina as represents zero or more repetitions of #. As # represents any letter, matches all the characters till
If there are multiple possible values of real information, then print the one with the largest length.
Example: if first message is construction and second_message is co#St # matches with n and S matches with s and struc. The real_information would be const or construct. Then, the real_information is construct as it has larger length than const.
Input
The first line of input contains a string representing first_message
The second line of input contains a string representing second_message
Output
The output should be a single line containing a string representing the real_information
Constraints
A maximum of one $ will be given in the second_message which contains the encrypted real_information
Explanation
For example, if the given first message = "hellloclient and second_message = "h#l$o"
hin second_message matches with h in first_message
#in second_message matches with e in first message as ## represents any letter.
I in second message matches with I in first_message
insecondmessagematcheswithIlinfirstmessageas represents represents zero or more repetitions of previous character of $, that is I. So, it matches the I characters that are in first message till o, that is II.
o in second_message matches with o in first_message
So, the real information is helllo.
The output should be helllo.
Sample Input 1
hellloclient
h#1$o
Sample Output 1
helllo
Sample Input 2
helloclient he#$i
Sample Output 2
hellocli
Sample Input 3
clientiseverythingisok
C#$1
Sample Output 3
clientiseverythingi
give me the efficient python code for this
To solve this problem efficiently, we need to simulate the matching process by considering the special characters # and $ in the second_message and their corresponding matching rules with the first_message.
Here’s the step-by-step approach:
Identify the Position of $: Find the position of $ in the second_message. If there is no $, then we can match the second_message directly with the first_message according to the rules.
Split the second_message: If there is a $, split the second_message into prefix (before $) and suffix (after $).
Match the Prefix:
Start matching the prefix part of the second_message with the first_message from the beginning.
For each character in the prefix:
If the character is #, it matches any character in the first_message.
If it is a normal character, it must match exactly with the corresponding character in the first_message.
If there is a mismatch, the matching process stops, and we return an empty string.
Process the $:
If Previous Character is #:
$ can match any number of characters (including zero characters) in the first_message.
We attempt to match the suffix starting from every possible position after the prefix in the first_message to find the longest possible match.
If Previous Character is a Regular Character:
$ matches zero or more repetitions of the previous character in the first_message.
We find the maximum stretch of the previous character in the first_message starting from the current position.
We attempt to match the suffix starting from the end of this stretch.
Match the Suffix:
After processing $, we match the suffix part of the second_message with the first_message.
For each character in the suffix:
If the character is #, it matches any character.
If it is a normal character, it must match exactly with the corresponding character in the first_message.
If there is a mismatch, we backtrack and try a shorter match for $.
Determine the Longest Match:
We keep track of the maximum length of the matched string.
If there are multiple possible matches, we select the one with the largest length.
Return the Real Information:
After finding the longest possible match, we extract the substring from the first_message that represents the real_information.
Below is the efficient Python code implementing the above logic:
def find_real_information(first_message, second_message):
pos_dollar = second_message.find('$')
len_first = len(first_message)
len_second = len(second_message)
if pos_dollar == -1:
# No $, simple match
index_i = 0
real_info = ''
for index_j in range(len_second):
if index_i >= len_first:
return ''
if second_message[index_j] == '#':
real_info += first_message[index_i]
index_i +=1
else:
if first_message[index_i] != second_message[index_j]:
return ''
real_info += first_message[index_i]
index_i +=1
return real_info
else:
# There is a $
prefix_second = second_message[:pos_dollar]
suffix_second = second_message[pos_dollar+1:]
suffix_len = len(suffix_second)
# Match Prefix
index_i = 0
for index_j in range(pos_dollar):
if index_i >= len_first:
return ''
if second_message[index_j] == '#':
index_i +=1
else:
if first_message[index_i] != second_message[index_j]:
return ''
index_i +=1
# After matching prefix, index_i is at the position in first_message after prefix
# Process $
previous_char = second_message[pos_dollar -1] if pos_dollar >0 else ''
max_length = -1
result = ''
if previous_char == '#':
# $ matches any zero or more letters
# Try to match the suffix from positions len_first - len(suffix_second) down to index_i
max_possible_k = len_first - suffix_len
for k in range(len_first, index_i-1, -1):
if k < index_i:
continue
if k + suffix_len > len_first:
continue
# Try matching suffix from position k
match = True
for m in range(suffix_len):
if suffix_second[m] == '#':
continue
if first_message[k + m] != suffix_second[m]:
match = False
break
if match:
total_length = k + suffix_len
if total_length > max_length:
max_length = total_length
result = first_message[:total_length]
break # Since we want the longest, we can break here
else:
# $ matches zero or more repetitions of previous_char
c = previous_char
k = index_i
while k < len_first and first_message[k] == c:
k +=1
max_k = k
for k in range(max_k, index_i-1, -1):
if k + suffix_len > len_first:
continue
# Try matching suffix from position k
match = True
for m in range(suffix_len):
if suffix_second[m] == '#':
continue
if first_message[k + m] != suffix_second[m]:
match = False
break
if match:
total_length = k + suffix_len
if total_length > max_length:
max_length = total_length
result = first_message[:total_length]
break # Since we want the longest, we can break here
return result
return ''
# Read input
first_message = input().strip()
second_message = input().strip()
# Invoke the function
real_info = find_real_information(first_message, second_message)
# Print the output
print(real_info)