Question: write a c + + code for this: Form The String Problem Description Sara, a school - aged child with a keen interest in playing
write a c code for this:
Form The String
Problem Description
Sara, a schoolaged child with a keen interest in playing with strings, decided to challenge herself on a boring day. She collected N substrings and a main string with the goal of constructing the main string using these substrings. Each substring incurs a specific cost every time it is used. Sara decided to use only those substrings that are part of the given main string. In cases where characters repeat upon concatenation of the substrings, she can remove the unnecessary characters, though the cost remains the same.
For example, main string "adkmu"
sub strings
adkm
ad
dk
kmu
Optimised solution will be using the strings adkm kmu which will becomeadkmkmuSara will remove the characterskm and the total cost incurred will be
Please help her achieve this with the minimum cost.
Constraints
length of main string
number of sub strings
cost of each sub string
length of each sub string length of main string
The main string, substrings will be having only lowercase alphabets.
Input
First line consists of an integerNdenoting the number of sub strings.
NextNlines consist of the substrings.
Last line consists of main string.
Output
Print an integer denoting the minimum cost within which we can form the main string. Print "Impossible" if it is impossible to form main string with the given set of sub strings.
Time Limit secs
Examples
Example
Input
evi
vta
co
dev
vit
odv
d
de
itaa
a
codevita
Output
Explanation
For forming the given main string, concatenate the sub strings co de vit, a which will incur a cost of which is the minimum possible.
Example
Input
lo
wor
hel
orld
lowor
ello
orl
orld
helloworld
Output
Explanation
For forming the given main string, concatenate the sub strings hel lowor, orld which will form the stringhellowororld From this string, we can remove the extra letters or to form the stringhelloworldThe cost incurred will be which is the minimum possible.
Example
Input
hyd
eraa
bad
pqrs
hyderabad
Output
Impossible
Explanation
As the substringeraais not present in the main string, hence we can't use it Thus, we cannot form the main stringhyderabadwith the given substrings. Hence, print "Impossible".
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
