TemplateMatching
SRM 417 · 2008-09-10 · by Pawa
Problem Statement
In this problem you will be given a
For example, if S = "something", prefix = "awesome", and suffix = "ingenious", than the prefix score of S is 4 (the matched characters are "some") and the suffix score is 3 (the matched characters are "ing").
The match score of a string S with respect to a given template is the sum of its prefix and suffix match scores. Find the non-empty substring of text with the maximal match score according to the template (prefix, suffix). In case of a tie, return the substring with the maximal prefix score. If there are still several candidates, return one that comes first lexicographically.
Notes
- String A comes before string B lexicographically if A is a proper prefix of B, or if A has a smaller character at the first position where the strings differ. When comparing the characters, refer to the following list of characters in ascending order: ' ', 'a', 'b', ..., 'z'.
Constraints
- text will contain between 1 and 50 characters, inclusive.
- prefix will contain between 1 and 50 characters, inclusive.
- suffix will contain between 1 and 50 characters, inclusive.
- text, prefix and suffix will contain only lowercase letters ('a'-'z') and spaces (' ').
"something" "awesome" "ingenious" Returns: "something"
The example from the problem statement.
"havka" "eto" "papstvo" Returns: "a"
The return value must be non-empty string, so the correct answer is "a".
"a" "b" "c" Returns: "a"
The return value must be non empty string, so the correct answer is "a".
"abracadabra" "habrahabr" "bracket" Returns: "abrac"
"mississippi" "promise" "piccolo" Returns: "ippi"
Submissions are judged against all 111 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class TemplateMatching with a public method string bestMatch(string text, string prefix, string suffix) · 111 test cases · 2 s / 256 MB per case