AtCoder Regular Contest 098 C - Attention DP思想,寻找相邻关系

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.

Constraints

  • 2N3×105
  • |S|=N
  • Si is E or W.

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.


Sample Input 1

Copy
5
WEEWW

Sample Output 1

Copy
1

Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.


Sample Input 2

Copy
12
WEWEWEEEWWWE

Sample Output 2

Copy
4

Sample Input 3

Copy
8
WWWWWEEE

Sample Output 3

3

题意:长度为n的由人组成的队列,队列中每个人向西或者向东看,现在从n个人中选出一个领导者,让队列中其他的人都朝向他看,如果不朝向他的则改变方向从而朝向他,求出所有的人朝向他的话,最小改变方向的人数是多少

思路:先计算当领导者为第一个人的时候,朝向他的人有多少,然后枚举领导者,计算他的人的前一个即可,为什么不用计算朝向他的后一个人呢,因为当领导者为第一个人,后面的都要求是E,所有他的后一个人在开始的时候已经计算过了,不需要再次计算,如果前一个人是E,则要l+1,如果领导者是W,则r-1,因为之前要求是E,但是如果是领导者的话则不做任何要求

#include 
using namespace std;
const int N = 3e5+5;
typedef long long ll;
char s[N];
int a[N];
int flag[N];
int main()
{
    int n;
    string s;
    cin>>n>>s;
    int l=0,r=0;
    for(int i=1;i


你可能感兴趣的:(ACM_想法,ACM_经典DP,Atcoder)