荷兰国旗问题
Time Limit:1000MS Memory Limit:65536K
Total Submit:33 Accepted:29
Description
荷兰国旗有三横条块构成,自上到下的三条块颜色依次为红、白、蓝。现有若干由红、白、蓝三种颜色的条块序列,要将它们重新排列使所有相同颜色的条块在一起。本问题要求将所有红色的条块放最左边、所有白色的条块放中间、所有蓝色的条块放最右边。
Input
第1行是一个正整数n(n<100),表示有n组测试数据。接下来有n行,每行有若干个由R,W,B三种字符构成的字符串序列,其中R,W和B分别表示红、白、蓝三种颜色的条块,每行最多有1000个字符。
Output
对输入中每行上由R,W,B三种字符构成的字符串序列,将它们重新排列使所有相同颜色的条块在一起,满足前述要求。
Sample Input
3
BBRRWBWRRR
RRRWWRWRB
RBRW
Sample Output
RRRRRWWBBB
RRRRRWWWB
RRWB
Source
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AK1171 {
class Program {
static void Main(string[] args) {
int n = int.Parse(Console.ReadLine());
while (n-- > 0) {
string s = Console.ReadLine();
int r = 0, w = 0, b = 0;
for (int i = 0; i < s.Length; i++) {
if (s[i] == 'R') r++;
if (s[i] == 'W') w++;
if (s[i] == 'B') b++;
}
for (int i = 0; i < r; i++)
Console.Write("R");
for (int i = 0; i < w; i++)
Console.Write("W");
for (int i = 0; i < b; i++)
Console.Write("B");
Console.WriteLine();
}
}
}
}