zcmu-1279 Sort photos(仔细读题)

 Description

Imagine you have a pile of n photos. Some of them are faced upwards and the others faced downwards. Your goal is to sort them so that all the photos are faced the same direction. The only operation you are allowed to do is to take any amount of the photos if their direction is same to the other direction. Write the program that calculates the minimum number of such operations needed to complete the sorting goal.

Input

There are multiple test cases in the input file.
For each test case, the first line is n (0<n<10^5) - the number of photos.
The second line is n characters "D" - faced down or "U" - faced up.

Output

For each test case there is a single integer number - minimal number of flip operations required to sort the photos pile.

Sample Input

5

UUDUU

4

UDUD

Sample Output

1

2

仔细读红字部分 

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

int main() {
    int n;
    char str[100005];
    while(scanf("%d",&n)!=EOF)
    {
        int x = 0,y = 0;
        scanf("%s",str);
        int len = strlen(str);
        for(int i = 0; i < len;i++)
        {
            if(str[i] == 'U' && str[i + 1] != 'U')
                x ++;
            if(str[i] == 'D' && str[i + 1] != 'D')
                y ++;
        }
        printf("%d\n",min(x,y));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hzyhfxt/article/details/81984535
今日推荐