从后往前冒泡交换,把'D'往后移

package org.niuke.solution38;

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        char[] s = sc.nextLine().toCharArray();
        int p = s.length - 1;
        int cnt = 0;
        while (p > 0) {
            if(s[p] == 'C'){
                int q = p - 1;
                while (q >= 0) {
                    if(s[q] == 'D'){
                        s[q] = 'C';
                        cnt += p - q;
                        p--;
                        break;
                    }
                    q--;
                }
                if(q <= 0){
                    break;
                }

            }else{
                p--;
            }
        }
        System.out.println(cnt);
    }
}