//\\__ hr1212 __//\\
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pi(a) printf("%d\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 1000100
#define MOD 1000000007
int n,m,mk[1000][1000],b[30];
char s[1000][1000];
vector<pair<char,int> > v;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
void dfs(int x,int y,char ch){
mk[x][y]=1;
int p,q,i;
f(i,0,4){
p=x+dx[i];q=y+dy[i];
if(p>=0 && p<n && q>=0 && q<m && s[p][q]==ch && !mk[p][q])
dfs(p,q,ch);
}
}
int cmp(pair<char,int> p,pair<char,int> q){
if(p.second!=q.second)
return p.second>q.second;
return p.first<q.first;
}
int main(){
int r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0;
ll ans=0;string p;
si(t);
f(l,1,t+1){
printf("World #%d\n",l);
v.clear();
sii(n,m);
clr(mk,0);
clr(b,0);
f(i,0,n){
scanf("%s",s[i]);
}
f(i,0,n){
f(j,0,m){
if(!mk[i][j]){
dfs(i,j,s[i][j]);
b[s[i][j]-'a']++;
}
}
}
f(i,0,26){
if(b[i])
v.pb(mp(i+'a',b[i]));
}
sort(all(v),cmp);
f(i,0,v.size()){
printf("%c: %d\n",v[i].first,v[i].second);
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pi(a) printf("%d\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 1000100
#define MOD 1000000007
int n,m,mk[1000][1000],b[30];
char s[1000][1000];
vector<pair<char,int> > v;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};
void dfs(int x,int y,char ch){
mk[x][y]=1;
int p,q,i;
f(i,0,4){
p=x+dx[i];q=y+dy[i];
if(p>=0 && p<n && q>=0 && q<m && s[p][q]==ch && !mk[p][q])
dfs(p,q,ch);
}
}
int cmp(pair<char,int> p,pair<char,int> q){
if(p.second!=q.second)
return p.second>q.second;
return p.first<q.first;
}
int main(){
int r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0;
ll ans=0;string p;
si(t);
f(l,1,t+1){
printf("World #%d\n",l);
v.clear();
sii(n,m);
clr(mk,0);
clr(b,0);
f(i,0,n){
scanf("%s",s[i]);
}
f(i,0,n){
f(j,0,m){
if(!mk[i][j]){
dfs(i,j,s[i][j]);
b[s[i][j]-'a']++;
}
}
}
f(i,0,26){
if(b[i])
v.pb(mp(i+'a',b[i]));
}
sort(all(v),cmp);
f(i,0,v.size()){
printf("%c: %d\n",v[i].first,v[i].second);
}
}
return 0;
}
No comments:
Post a Comment