SWUST OJ492: 荷兰国旗问题

题目描述

荷兰国旗的问题是重新排列一系列字符R,W和B(红色,白色和蓝色是荷兰国旗的颜色),以便所有R首先出现,W紧随其后,B最后。针对此问题设计线性且稳定的算法

输入

two lines, the first line is total of numbers characters R,W and B ,and the numbers less than 500005
the second line is random characters R,W and B

输出

a line, all the R’s come first, the W’s come next, and the B’s come last.

样例输入

10
WRRWRWBBRW

样例输出

RRRRWWWWBB
#include<stdio.h>
int main()
{
	int n;
	scanf("%d", &n);
	char a[500005];
	char r[500005], w[500005], b[500005];
	int i;
	int count = 0;
	int num = 0;
	int tmp = 0;
	scanf("%s", a);
	for (i = 0; i < n; i++)
	{
		if (a[i] == 'R')
		{
			r[count++] = a[i];
		}
		if (a[i] == 'W')
		{
			w[num++] = a[i];
		}
		if (a[i] == 'B')
		{
			b[tmp++] = a[i];
		}
	}
	for (i = 0; i < count; i++)
	{
		printf("%c", r[i]);
	}
	for (i = 0; i < num; i++)
	{
		printf("%c", w[i]);
	}
	for (i = 0; i < tmp; i++)
	{
		printf("%c", b[i]);
	}
	printf("\n");
	return 0;
}

上一篇:python小题目+快速排序(青春是丛林,是荒原,是阳光炙热的奔跑,是大雨滂沱的伫立。)


下一篇:P1420 最长连号