Cities

When playing the cities game each player has to name a city, and the next player has to name a city that starts with the last letter of the city that was just named. So, if someone said Moscow, the next player can say Warsaw as Moscow ends with w.
Given a game where each new city is separated from the previous one with a comma and a space, your task is to find out if it’s a valid game. Otherwise, you should tell that someone has already lost.
The input contains a single line where each named city is separated by a ', ' from the previous one.
The program should output Go on if the game is valid so far, and Stop! if someone messed up in the chain of cities.
Input
Output
Moscow, Warsaw, West Jordan, Nashville
Go on
Warsaw, Moscow
Stop!
Moscow, Amsterdam
Stop!
 

Constraints

Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue