ACM计数问题

5 篇文章 0 订阅

count

TimeLimit: 1 Second MemoryLimit: 32 Megabyte

Totalsubmit: 699 Accepted: 158

Description

Given a number of strings, can you find how many times a string appear in the input?

Input

The input contains multiple test cases. Each case begins with a integer N(the number of strings you will get, N<=100000), followed by N lines, each consists of a string.

Output

For each test case, print "Case K:" where K is the Kth case. K begins with 1. Then print the times(T) a string appears and the number(M) of strings that appear T times. Don't print T or M where M<=0. The output is ordered by T. The length of each strings won't longer than 20.

Sample Input


5
BBA
BBA
BEA
DEC
CCF

Sample Output


Case 1:
1 3
2 1

 

 

 

C预言实现如下;

 

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

 

typedef struct ee

{

       char str[21];

}node;

node a[100001];

 

 

 

 

void show(int sum[],int m)

{

       int i;

       for( i = 1; i <= m; i++)

       {

              if( sum[i] != 0)

                     printf("%d %d\n",i,sum[i]);

       }

}

 

int cmp( const void *n1, const void *n2)

{

       node *a = (node*) n1;

       node *b = (node*) n2;

       char *str1 = a->str;

       char *str2 = b->str;

       return strcmp(str1,str2);

}

 

int main()

{

 

       int n,num,k;

       char s[21];

       k = 1;

       int sum[100001];

       while( scanf("%d",&n) != EOF)

       {

              if( n <= 0 || n > 100000)

                     break;

              memset(sum,0,100001);

              num = 1;

              int i,temp;

              for( i = 0; i < n; i++ )

              {

                     scanf("%s",a[i].str);

              }

              qsort(a,n,sizeof(a[0]),cmp);

 

              strcpy(s,a[0].str);

              temp = 1;

              for( i = 1; i < n; i++ )

              {

                     if( strcmp(s,a[i].str) == 0)

                            temp++;

                     else

                     {

                            strcpy(s,a[i].str);

                            sum[temp]++;

                            temp = 1;

                     }

              }

              sum[temp]++;

              printf("Case %d:\n",k++);

              show(sum,n);

       }

       return 0;

}

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值