CommentNest
SRM 268 · 2005-10-18 · by dgoodman
Problem Statement
More formally we can define an MLC as follows:
- A <Nester> is a string, possibly empty, that contains no "/*" and no "*/" OR a Nester consists of three concatenated strings, <Nester> <MLC> <Nester>
- An <MLC> is a string that consists of three concatenated substrings, "/*" + <Nester> + "*/" (Notice that a <Nester> can be an empty string).
Create a class CommentNest that contains a
method whatsLeft that takes a
Constraints
- lines contains between 1 and 20 elements, inclusive.
- Each element of lines contains between 0 and 50 characters, inclusive.
- Each character in each element of lines is either '/', '*', or a lowercase letter ('a'-'z').
{"abc","def"}
Returns: 8
Nothing is removed. There are 6 characters plus 2 end-of-lines.
{"a//bc/*/d", "", "x/*/b"}
Returns: 7
/*/d@@x/*/ is an MLC (where @ denotes end-of-line), since /d@@x/ is a Nester. That leaves 6 characters from the lines plus 1 end-of-line character.
{"a/***b///**/*/"}
Returns: 2
/***b///**/*/ is an MLC, since its **b///**/ is a Nester since it consists of **b// + /**/ + which is Nester MLC Nester.
{"/*/*/abc//*xyz*/*" }
Returns: 6
/*/*/ is an MLC. No longer MLC starts with the first /*. Continuing, we look for an MLC that starts at the a, at the b, at the c, at the /. When we get to the next /, we find /*xyz*/ is the longest MLC that starts there. We continue at the final * which cannot begin a comment. After all the comments we found have been removed, we are left with abc/ and *@ which is a total of 6 characters.
{"/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*", "/*abc*/"}
Returns: 3
The MLC that starts at the first character includes all but the final * from the first line. It contains the character '/' nested 12 deep in comments.
{"/*/*/"}
Returns: 1
/*/*/ is a comment so after it is removed all that remains is the newline.
{"/*/*/","/*/bc/*/d"}
Returns: 2
It is still true that /*/*/ is a comment, but there is now a longer one that starts at the same position: /*/*/@/*/bc/*/ is a comment because the sequence /*/@/*/ is a nested comment. This leaves d and the final newline after the comments are removed.
Submissions are judged against all 74 archived test cases, of which 7 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class CommentNest with a public method int whatsLeft(vector<string> lines) · 74 test cases · 2 s / 256 MB per case