无聊的刷题笔记

Attention

AtCoder Regular Contest 098 C

Problem Statement
There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if Si= E, and west if Si= W.

You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing.

The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions.

Input
Input is given from Standard Input in the following format:

N
S

Output
Print the minimum number of people who have to change their directions.

扫两遍

n = int(raw_input())
S = raw_input().strip()

face_E = 0
face_W = 0
for i in S:
if i=='E':
face_E+=1
else:
face_W+=1

change = n
left_E = 0
left_W = 0
right_E = face_E
right_W = face_W
for people in S:
if people == 'W':
right_W -= 1
change = min(change, left_W + right_E)
left_W += 1
else:
right_E -= 1
change = min(change, left_W + right_E)
left_E += 1

print change