-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path10336 - Rank the Languages.cpp
86 lines (71 loc) · 1.46 KB
/
10336 - Rank the Languages.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int row,col;
char c[505][505];
int V[505][505];
int dx[]={0, 0, 1,-1};
int dy[]={1,-1, 0, 0};
struct Print
{
char ch;
int rank;
}sol[30];
bool cmp (Print x, Print y)
{
if ( x.rank > y.rank ) return true;
if ( x.rank == y.rank && x.ch < y.ch ) return true;
return false;
}
void dfs(int x,int y)
{
V[x][y]=1;
int i;
for(i=0;i<4;i++)
{
int a=x+dx[i],b=y+dy[i];
if(a>=0 && a<row && b>=0 && b<col && !V[a][b] && c[x][y]==c[a][b])dfs(a,b);
}
}
int main()
{
int T,cs=1;
int i,j;
int CNT[200];
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&row,&col);
for(i=0;i<row;i++)scanf("%s",c[i]);
memset(V,0,sizeof V);
memset(CNT,0,sizeof CNT);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
{
if(!V[i][j])
{
dfs(i,j);
CNT[ c[i][j] ]++;
}
}
int len=0;
for(i='a';i<='z';i++)
{
if(CNT[i])
{
sol[len].ch=i;
sol[len].rank=CNT[i];
len++;
}
}
sort(sol,sol+len,cmp);
printf("World #%d\n",cs++);
for(i=0;i<len;i++)
{
printf("%c: %d\n",sol[i].ch,sol[i].rank);
}
}
return 0;
}