把由字符‘C’和‘D’混合的字符串分成前半部分全是‘C’或‘D’所需要的最少步骤

#include 
#include 
#include 
using namespace std;
int calcSwapCount(char bc, char ec, char *str, int n)
{
    int count = 0;
    int begin = 0, end = n - 1;
    while (begin < end)
    {
        if (str[begin] == bc)
            begin++;
        else if (str[end] == ec)
            end--;
        else if (str[begin] == ec && str[end] == bc)
        {
            count += end - begin;
            begin++;
            end--;
        }
    }
    return count;
}

int main()
{
    char str[51];
    scanf("%s", &str);
    int n = strlen(str);

    printf("%d\n", min(calcSwapCount('C', 'D', str, n), calcSwapCount('D', 'C', str, n)));

    return 0;
}

你可能感兴趣的:(c/c++)