루미 알고리즘

#1342골드Dance Pad

PO 2014 온라인 예선코딩출제 Johan Sannemo영어 원문이 문제가 어렵다면 — 배우는 순서 보기

영어 원문입니다. 원문의 수식·표를 글자로 옮겨 모양이 다를 수 있습니다. 정확한 내용은 원문 페이지에서 확인하세요. 원문 페이지 열기 (새 창)

Problem

A dance pad consists of four arrows pointing up, right, down and left. When playing a dance game you are given a sequence of arrows to step on to the beat of the music. Sometimes two arrows can be shown for the same beat, so you have to make a jump to step on them. You can always remain on an arrow with a foot even if you don't need to press it for a beat.

(그림은 원문 저장소에서 볼 수 있습니다)

Figure 1: A dance pad.

Johan jumps around on his dance pad almost every day, but finds it hard to keep up with moving his feet sometimes. Therefore he wants some help with playing a song optimally. That means moving his feet to new arrows as few times as possible.

At the start of the game, his left foot is on the left arrow, and his right foot on the right arrow.

Given a song and which one or two arrows have to be pressed at each beat, compute the minimum number of times Johan must move a foot to another arrow. Moving both his feet in a jump still counts as two movements.

Input

The first line contains the integer N (1 ≤ N ≤ 10000), the number of beats in the song.

This is followed by N lines, one line for each beat. Each beat consists of a string with one or two letters of U, H, N or V meaning “up”, “right”, “down”, “left”, respectively. These are the arrows that must be pressed during the beat.

Output

Output the an integer – the least number of moves Johan must make.

Scoring

Your solution will be tested on a set of test case groups. To get the points for a group, you need to pass all the test cases in the group.

GroupPointsConstraints
120N ≤ 10 and Johan only needs to press one arrow per beat.
230Johan only needs to press one arrow per beat.
350No further constraints.

Explanation of sample 1

Johan starts with his feet on the left and right arrows (as always). A possible optimal solution is the following. For the first beat, he moves the right foot to the up arrow. Then he moves the right foot again to the down arrow. For the next two moves he doesn't move his feet, since he already have a foot on the left arrow, and finally he moves an arbitrary foot to the right arrow. This requires 3 moves.

예제

예제 1

입력
5
U
N
V
V
H
출력
3

예제 2

입력
4
UN
VH
U
N
출력
6

출처 표기

출처: Programmeringsolympiaden 2014년 온라인 예선 · 원문 페이지 · 이용 조건 CC BY-SA · LaTeX 원문을 화면용 글로 옮겼습니다(수식·표의 모양이 다를 수 있습니다). 내용은 고치지 않았습니다.

크리에이티브 커먼즈 저작자표시-동일조건변경허락.

문제 출처: 한국정보올림피아드(KOI) (CC BY-NC-SA 4.0) · International Olympiad in Informatics (IOI) (CC BY) · Centre for Education in Mathematics and Computing (CEMC), University of Waterloo (CC BY-NC 4.0) · Programmeringsolympiaden (CC BY-SA) · CSES Problem Set (Antti Laaksonen) (CC BY-NC-SA 4.0). 루미 알고리즘은 위 단체들과 관계없는 별도의 서비스이며, 자료는 원문 그대로 싣습니다. 문제마다 출처와 이용 조건을 따로 표기합니다.