Red And Green

 

 

#include <stdio.h>
#include <string.h>

#define LENGTH 50

/*
 * 1.字符序列中有一個字符確定是分界點,它的左邊全爲紅色,右邊全爲綠色
 * 2.所以  該點的左邊的綠色要翻轉爲紅色  該點右邊的紅色要翻轉成綠色
 * 3.統計每一個點須要翻轉的次數  從中找出最小的次數即爲答案
 * */

int main() {
    char brick[LENGTH];
    int len;
    scanf("%s", brick);
    len = strlen(brick);
    int leftGreen[LENGTH] = {0};
    int rigthRed[LENGTH] = {0};

    for (int i = 1; i < len; ++i) {
        leftGreen[i] = leftGreen[i - 1] + (brick[i - 1] == 'G' ? 1 : 0);
    }
    for (int j = len - 2; j >= 0; --j) {
        rigthRed[j] = rigthRed[j + 1] + (brick[j + 1] == 'R' ? 1 : 0);
    }
    int res = len;
    for (int k = 0; k < len; ++k) {
        res = (res > (leftGreen[k] + rigthRed[k]) ? (leftGreen[k] + rigthRed[k]) : res);
    }

    printf("%d",res);
}
相關文章
相關標籤/搜索