ACM (UVa) : 10336


#include <iostream>
#include <stack>
#include <algorithm>

using namespace std;

char a [100] [100]; // global
char c;

void connected (int i, int j)
{
	a [i] [j] = '0';

	if ( a [i] [j - 1] == c )
		connected ( i, j - 1 );

	if ( a [i - 1] [j] == c )
		connected ( i - 1, j );

	if ( a [i] [j + 1] == c )
		connected ( i, j + 1 );

	if ( a [i + 1] [j] == c ) 
		connected ( i + 1, j );
}

int main ()
{
	int test;
	int Case = 0;

	scanf ("%d", &test);

	while ( test-- ) {

		int alpha [26] = {0}; // frequency

		int i, j; // iterator
		for ( i = 0; i < 20; i++ ) {
			for ( j = 0; j < 20; j++ )
				a [i] [j] = 0;
		}

		int h, w;

		scanf ("%d %d", &h, &w);
		scanf ("%c", &c);

		for ( i = 1; i <= h; i++ ) {
			j = 1;
			while ( j <= w )
				scanf ("%c", &a [i] [j++]);
			scanf ("%c", &c);
		}

		for ( i = 1; i <= h; i++ ) {
			for ( j = 1; j <= w; j++ ) {
				if ( a [i] [j] != '0' ) {
					c = a [i] [j];
					alpha [a [i] [j] - 97]++;
					connected (i , j);
				}
			}
		}

		int max;
		int index;

		printf("World #%d\n", ++Case); 

		do {
			max = 0;
			for ( i = 0; i < 26; i++ ) {
				if ( max < alpha [i] ) {
					max = alpha [i];
					index = i;
				}
			}
			if ( max != 0 ) {
				printf("%c: %d\n", index+97, max);
				alpha [index] = 0;
			}
		} while ( max != 0 );
	}

	return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s