Liars
InterviewTime limit2sMemory limit512 MB
Given a circular string of answers about whether the right neighbor is a liar, find the minimum number of liars consistent with all answers, or -1.
- Level
Medium4 of 10
- Topics
- Brute force, Implementation, Array
- Solved
- No attempts yet
Problem
people at an algorithm camp gather at the front of the classroom and sit in a circle. They are numbered through counterclockwise.
Every participant is either honest or a liar. An honest person always tells the truth, and a liar always tells a lie.
The host asked each person whether the person sitting to their right is a liar. An honest person answers truthfully and a liar answers falsely. Both an honest person and a liar may refuse to answer.
Given the answers, write a program that prints the smallest possible number of liars if some assignment of honest people and liars produces exactly those answers, and if no assignment does.
Input
The first line contains the number of people . ()
The second line contains the answers as a string of length . The -th character from the left, counting from , is the answer of person .
An answer of L means the person said the one to their right is a liar, H means the person said that one is honest, and ? means the person refused to answer.
Output
If some assignment of honest people and liars produces the given answers, print the smallest possible number of liars. Otherwise print .
Note
The numbering runs counterclockwise, so the person sitting to the right of person is person .