Please Take My Gift 2
InterviewTime limit2sMemory limit256 MB
Given a 1 by N arrow map where every walk stays inside, place the fewest gifts so that a walk from any starting cell hits a gift.
- Level
Medium7 of 10
- Topics
- Graph, Greedy, Implementation, DFS
- Solved
- No attempts yet
Problem
Ukje is an avid fan of Gusagwa. Today Ukje wants to hand a gift to Gusagwa. After watching for several days, Ukje has learned the complete movement pattern of Gusagwa.
The place where Gusagwa stays is a rectangular map of size , divided into square cells of size . The position of Gusagwa is written as , which means the th cell from the left.
Each cell holds either E or W. When Gusagwa is at , Gusagwa teleports to if the cell shows E and to if it shows W. Gusagwa never gets tired and keeps moving.
Ukje does not know the starting position of Gusagwa. Ukje wants to place gifts on cells so that Gusagwa picks up a gift during the walk no matter where the walk starts. When Gusagwa enters a cell with a gift, Gusagwa always picks it up. Write a program that finds the smallest number of cells that guarantees this.
Input
The first line has the length of the map. ()
The second line has the map as a string of length . The string consists only of E and W.
A walk that follows the map never leaves the map.
Output
Print the smallest number of cells that need gifts on the first line.