题目: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
Submit | Status |