HEUOJ1001 count

本文提供了一个HEUOJ在线评测系统的题目解答,该题目要求统计输入字符串出现次数并按出现频率排序输出。使用C语言实现,通过结构体数组存储字符串,并借助qsort函数两次排序实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:HEUOJ:http://acm.hrbeu.edu.cn/index.php?act=problem&id=1001&cid=18

 

现在发现总是忘记数组初始化,很烦额~~~

 

AC代码:

#include<stdio.h> #include<string.h> #include<stdlib.h> #define MAX 100005 typedef struct node{ char s[25]; }node; node ss[MAX]; int num[MAX]; int cmp1(const void *a,const void *b){ struct node *c=(struct node *)a; struct node *d=(struct node *)b; return strcmp(c->s,d->s); } int cmp2(const void *a,const void *b){ return *(int *)a-*(int *)b; } int main(){ int N,i,j,temp,count,t=0; while(scanf("%d",&N)!=EOF){ for(i=0;i<N;i++){ scanf("%s",ss[i].s); } t++; printf("Case %d:/n",t); qsort(ss,N,sizeof(ss[0].s),cmp1); j=0; for(i=0;i<MAX;i++){ num[i]=0; } for(i=0;i<N;){ temp=i; while(i<N&&strcmp(ss[temp].s,ss[i].s)==0){ num[j]+=1; i++; } j++; } count=j; qsort(num,count,sizeof(int),cmp2); for(i=0;i<count;){ printf("%d ",num[i]); temp=0; j=i; while(i<count&&num[j]==num[i]){ temp++; i++; } printf("%d/n",temp); } } return 0; }

 

原题目:

count

TimeLimit: 1 Second   MemoryLimit: 32 Megabyte

Totalsubmit: 116   Accepted: 30  

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

SubmitStatus

转载于:https://www.cnblogs.com/xiajun/archive/2011/03/27/2298659.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值