SearchBox
SRM 361 · 2007-08-01 · by slex
Problem Statement
You are writing a simple text editor, and one of the features you need to implement is a text search. Given a pattern, the search mechanism should return the position of its first occurrence, starting from the current position, or indicate that the pattern cannot be found. Searches are case sensitive, and do not wrap. The search dialog contains a text box and a checkbox labeled "Whole Word". When the "Whole Word" option is selected, the matched sequence must either be preceded by a space to its left or be at the very beginning of the text. Similarly, it must also either be followed by a space to its right or be at the very end of the text.
You are given a
Constraints
- text will contain between 1 and 50 characters, inclusive.
- text will contain only letters ('a'-'z', 'A'-'Z') and spaces.
- search will contain between 1 and 50 characters, inclusive.
- search will contain only letters ('a'-'z', 'A'-'Z').
- wholeWord will contain a single letter 'Y' or 'N'.
- start will be between 0 and N-1, inclusive, where N is the number of characters in text.
"We dont need no education" "ed" "N" 13 Returns: 16
The whole text has two occurrences of "ed", but since we start the search at the 13th character we find the second one.
"We dont need no thought control" "We" "Y" 0 Returns: 0
"We" is exactly at the beginning of the text.
"No dark sarcasm in the classroom" "The" "Y" 5 Returns: -1
Remember that the search is case sensitive.
"Teachers leave them kids alone" "kid" "Y" 1 Returns: -1
With the "Whole Word" option selected the "kid" won't be matched even though it's present in "kids".
"All in all its just another brick in the wall" "all" "Y" 9 Returns: -1
Submissions are judged against all 161 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class SearchBox with a public method int find(string text, string search, string wholeWord, int start) · 161 test cases · 2 s / 256 MB per case