Problem A
Rank the Languages
Input: standard input
Output: standard output
Time Limit: 2 seconds
Memory Limit: 32 MB
You might have noticed that English and Spanish are spoken in many areas all over the world. Now it would be nice to rank all languages according to the number of states where they are spoken.
Problem
You're given a map which shows the states and the languages where they are spoken. Look at the following map:
ttuuttdd
ttuuttdd
uuttuudd
uuttuudd
The map is read like this: Every letter stands for a language and states are defined as connected areas with the same letter. Two letters are "connected" if one is at left, at right, above or below the other one. So in the above map, there are three states where the language "t
" is spoken, three where "u
" is spoken and one state where people speak "d
".
Your job is to determine the number of states for each language and print the results in a certain order.
Input
The first line contains the number of test cases N. Each test case consists of a line with two numbers H and W, which are the height and the width of the map. Then follow H lines with a string of W letters. Those letters will only be lowercase letters from "a
" to "z
".
Output
For each test case print "World #n
", where n is the number of the test case. After that print a line for each language that appears in the test case, which contains the language, a colon, a space and the number of states, where that language is spoken. These lines have to be ordered decreasingly by the number of states. If two languages are spoken in the same number of states, they have to appear alphabetically, which means language "i
" comes before language "q
", for example.
Sample Input
2
4 8
ttuuttdd
ttuuttdd
uuttuudd
uuttuudd
9 9
bbbbbbbbb
aaaaaaaab
bbbbbbbab
baaaaacab
bacccccab
bacbbbcab
bacccccab
baaaaaaab
bbbbbbbbb
Sample Output
World #1
t: 3
u: 3
d: 1
World #2
b: 2
a: 1
c: 1题目大意:
给你一个矩阵,代表一大块区域,每种小写字母代表此区域讲一种语言,每种语言有一定的分布区域,且不一定在一块区域,问你每种语言有多少块区域。先按照区域个数输出,若个数相等在按照字典顺序输出。
解题思路:
DFS,只是先从(0,0)这个位置开始遍历,并且对此语言计数器加一,没有访问过的就进入DFS,然后把四周字母相同的置为访问,否则什么也不做。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
struct node{
char lang;
int cnt;
node(char lang0 ='0',int cnt0=0){lang=lang0,cnt=cnt0;}
}langNumber[26];
int n,m;
bool visited[1000][1000];
string mymap[1000];
bool cmp(node a,node b){
if(a.cnt!=b.cnt) return a.cnt>b.cnt;
else return a.lang<b.lang;
}
void initial(){
memset(visited,false,sizeof(visited));
for(int i=0;i<26;i++){
langNumber[i].lang=i+'a';
langNumber[i].cnt=0;
}
}
void input(){
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++) cin>>mymap[i];
}
void dfs(char c,int pi,int pj){
if (!visited[pi][pj]&&pi>=0&&pi<n&&pj>=0&&pj<m){
if (mymap[pi][pj] == c){
visited[pi][pj] = true;
dfs(c, pi + 1, pj);
dfs(c, pi - 1, pj);
dfs(c, pi, pj + 1);
dfs(c, pi, pj - 1);
}
}
}
void output(){
sort(langNumber,langNumber+26,cmp);
for(int i=0;i<26;i++){
if(langNumber[i].cnt>0)
printf("%c: %d\n",langNumber[i].lang,langNumber[i].cnt);
}
}
int main(){
int t,casen=0;
scanf("%d",&t);
while(t--){
initial();
input();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(!visited[i][j]){
langNumber[mymap[i][j]-'a'].cnt++;
dfs(mymap[i][j],i,j);
}
}
}
printf("World #%d\n",++casen);
output();
}
return 0;
}
Problem A
Rank the Languages
Input: standard input
Output: standard output
Time Limit: 2 seconds
Memory Limit: 32 MB
You might have noticed that English and Spanish are spoken in many areas all over the world. Now it would be nice to rank all languages according to the number of states where they are spoken.
Problem
You're given a map which shows the states and the languages where they are spoken. Look at the following map:
ttuuttdd
ttuuttdd
uuttuudd
uuttuudd
The map is read like this: Every letter stands for a language and states are defined as connected areas with the same letter. Two letters are "connected" if one is at left, at right, above or below the other one. So in the above map, there are three states where the language "t
" is spoken, three where "u
" is spoken and one state where people speak "d
".
Your job is to determine the number of states for each language and print the results in a certain order.
Input
The first line contains the number of test cases N. Each test case consists of a line with two numbers H and W, which are the height and the width of the map. Then follow H lines with a string of W letters. Those letters will only be lowercase letters from "a
" to "z
".
Output
For each test case print "World #n
", where n is the number of the test case. After that print a line for each language that appears in the test case, which contains the language, a colon, a space and the number of states, where that language is spoken. These lines have to be ordered decreasingly by the number of states. If two languages are spoken in the same number of states, they have to appear alphabetically, which means language "i
" comes before language "q
", for example.
Sample Input
2
4 8
ttuuttdd
ttuuttdd
uuttuudd
uuttuudd
9 9
bbbbbbbbb
aaaaaaaab
bbbbbbbab
baaaaacab
bacccccab
bacbbbcab
bacccccab
baaaaaaab
bbbbbbbbb
Sample Output
World #1
t: 3
u: 3
d: 1
World #2
b: 2
a: 1
c: 1