Sunday, 22 July 2012

UVa 10336 - Rank the Languages Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define LL long long
#define MX 100000
#define MOD 1000000007

char grid[1010][1010];
map<pair<int,int>,int>visit;
map<char,int>frequency;

void mark(int x,int y,char c)
{
    visit[MP(x,y)]=1;
    if(grid[x][y+1]==c && !visit[MP(x,y+1)])
        mark(x,y+1,c);
    if(grid[x][y-1]==c && !visit[MP(x,y-1)])
        mark(x,y-1,c);
    if(grid[x+1][y]==c && !visit[MP(x+1,y)])
        mark(x+1,y,c);
    if(grid[x-1][y]==c && !visit[MP(x-1,y)])
        mark(x-1,y,c);
}

main()
{
    int t,kk=1,n,m,i,j;
    char c;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        CLR(grid);
        visit.clear();
        frequency.clear();
        int max_freq=0;

        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
                cin>>grid[i][j];

        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
                if(!visit[MP(i,j)])
                {
                    frequency[grid[i][j]]++;
                    mark(i,j,grid[i][j]);
                    if(frequency[grid[i][j]]>max_freq)
                    max_freq=frequency[grid[i][j]];
                }

        cout<<"World #"<<kk++<<endl;
        for(i=max_freq;i>0;i--)
            for(j=97;j<=122;j++)
                {
                    c=j;
                    if(frequency[c]==i)
                    cout<<c<<": "<<i<<endl;
                }


    }
}

No comments:

Post a Comment