What a Simple Research

来源:互联网 发布:淘宝联盟导购和推广位 编辑:程序博客网 时间:2024/06/06 12:03

Problem F. What a Simple Research

Description

Peking University Student Folk MusicBand has a history of more than 90 years. They play Chinese traditional musicby Chinese traditional instruments, such as Pipa, Erhu and Guzheng, etc. DoctorLi is a member of that band, and also a former ACMer. Now he is doing someresearch on Chinese ancient music. Many Chinese ancient music has only five kindsof tones, which can be denoted by 'C','D','E','G', and 'A'. Given a piece of musicscore, Li wants to do some simple statistics.

Input

There are no more than 20 test cases.

In each test case:

The first line contains two integers nand m (2<= n,m <= 20), indicating that a piece of music score isrepresented by an n×m matrix of tones. Only 'C','D','E','G' and 'A' can appear in thematrix.

Then the n×m matrix follows.

The input ends with a line of "00".

Output

For each test case:

For each kind of tone shown in thematrix, calculate the appearing times of it, and print the result in descendingorder according to the appearing times. If more than one kind of tones has thesame appearing times, print them in the lexicographical order.

Sample Input

4 5

AGCDE

AGDDE

DDDDD

EEEEE

2 4

GADC

CDEE

0 0

SampleOutput

D 8 E 7 A 2 G 2 C 1

C 2 D 2 E 2 A 1 G 1

题意:求一个矩阵中各个字母出现的次数。比较简单,模拟求解。

Code:

#include<cstdio>#include<algorithm>using namespace std;struct node{char c;int x;}h[6];bool cmp(node a,node b){return a.x>b.x;}int main(){int n,m;scanf("%d%d",&n,&m);while (n!=0 && m!=0)        {char ch[100];for (int i=1;i<=5;i++) h[i].x=0;for (int i=1;i<=n;i++)                {scanf("%s",ch);for (int j=0;j<m;j++)                        {if (ch[j]=='A') h[1].x++;if (ch[j]=='C') h[2].x++;if (ch[j]=='D') h[3].x++;if (ch[j]=='E') h[4].x++;if (ch[j]=='G') h[5].x++;}}h[1].c='A',h[2].c='C',h[3].c='D',h[4].c='E',h[5].c='G';sort(h+1,h+6,cmp);int p=5;for (int i=1;i<=5;i++)                {if (h[i].x==0)                        {p=i-1;break;}}for (int i=1;i<=p-1;i++) printf("%c %d ",h[i].c,h[i].x);printf("%c %d\n",h[p].c,h[p].x);scanf("%d%d",&n,&m);}return 0;}

原创粉丝点击