Connection Status:
Competition Arena > Etiquette
SRM 123 · 2002-12-10 · by axchma
Class Name: Etiquette
Return Type: int
Method Name: numNonGent
Arg Types: (string)
Problem Statement

Problem Statement

Every gentleman of the nineteenth century knew the rule "Ladies first". The true gentlemen knew that there were three exceptions to this rule. One of them was that a gentleman didn't go behind a lady up the stairs.

Your task is, given a String describing people going up the stairs, return the number of people who are not true gentlemen. In more detail, the input String, read from left to right, will describe the order in which events happen on the stairs. The String will consist of characters 'L', 'G' and ' ' (space), where 'L' indicates a lady going up the stairs, 'G' indicates a gentleman going up the stairs and space (' ') characters indicate periods of time when nobody goes up the stairs (that is, the spaces divide one group of people from another). You need to count the number of "gentlemen" that allow a lady from the same group to go up the stairs before them.

Notes

  • Gaps between groups are assumed to be long enough. Hence, a gentleman who goes up the stairs later than a lady from a previous group doesn't necessarily count as not being a true gentleman (his status depends on his position relative the ladies in his own group).

Constraints

  • people will consist only of the characters 'L', 'G' and ' ' (space)
  • people will have between 0 and 50 characters inclusive
Examples
0)
"LGG"
Returns: 2
1)
"GGL LGG"
Returns: 2
2)
"GLL LL GG  G GGLL"
Returns: 0
3)
""
Returns: 0
4)
"LLL GGG"
Returns: 0
12)
"LL GG GLG GGL LG LG    GL"
Returns: 3

The first two groups consist of people of one gender, so there is no way to check if they know rules, hence we do not count anyone here. In the third group one gentleman doesn't know the exception rule, hence we count one non-true gentleman here. In the fourth group both gentlemen are true gentlemen, so we add zero here. In the fifth the gentleman is not a true gentleman, so we add one. The sixth group is the same as the fifth group, so we add one more. In the last group the gentleman is a true gentleman, so we do not count him.

13)
"L   GLGGGGGGGGGG LLGLLL L  LLG G G  LGG  L GL G GL"
Returns: 14

ten random test cases

Submissions are judged against all 65 archived test cases, of which 7 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class Etiquette with a public method int numNonGent(string people) · 65 test cases · 2 s / 256 MB per case

Submitting as anonymous