3 BBRRWBWRRR RRRWWRWRB RBRW
RRRRRWWBBB RRRRRWWWB RRWB
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main(){ int n; cin >> n; for(int icase = 0 ; icase < n; ++ icase){ string flag; cin >> flag; int leftIndex = 0,rightIndex = flag.length()-1; while(flag[leftIndex] == ‘R‘) leftIndex++; while(flag[rightIndex] == ‘B‘) rightIndex--; int currentIndex = leftIndex; while(currentIndex<= rightIndex){ if(flag[currentIndex] == ‘R‘) swap(flag[currentIndex++],flag[leftIndex++]); else if(flag[currentIndex] == ‘B‘) swap(flag[currentIndex],flag[rightIndex--]); else currentIndex++; } cout<<flag<<endl; } }
原文:http://www.cnblogs.com/xiongqiangcs/p/3639218.html